Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

XML Schema question

Posted on 2006-07-03
16
Medium Priority
?
455 Views
Last Modified: 2013-11-19
my application pulls data from the database and stores it into a datatable.
I need to display this data in xml format.

I've created an xml schema to format the xml:

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema id="products" targetNamespace="http://tempuri.org/products.xsd" elementFormDefault="qualified"
     xmlns="http://tempuri.org/products.xsd" xmlns:mstns="http://tempuri.org/products.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     
<xsd:element name="manufacturer">
            <xsd:complexType>
                  <xsd:choice minOccurs="1" maxOccurs="unbounded">
                        <xsd:element name="manufacturer" type="manufacturerType" />
                  </xsd:choice>
            </xsd:complexType>
      </xsd:element>
      <xsd:complexType name="manufacturerType">
            <xsd:sequence>
                  <xsd:element name="product" type="productType" minOccurs="1" maxOccurs="1" />
            </xsd:sequence>
            <xsd:attribute name="title" form="unqualified" type="xsd:string" />
      </xsd:complexType>
      <xsd:complexType name="productType">
            <xsd:attribute name="title" type="xsd:string" />
            <xsd:attribute name="count" type="xsd:integer" />
      </xsd:complexType>
</xsd:schema>

The resulting xml should be

<?xml version="1.0" standalone="yes" ?>
<root>
              <MANUFACTURER title="3Com">
                  <PRODUCT title="Part1" count="2" />
                    <PRODUCT title="Part2" count="1" />
            </MANUFACTURER>
              
             <MANUFACTURER title="Test Corp.">
                  <PRODUCT title="Book1" count="3" />
            </MANUFACTURER>
      
</root>


but all I'm getting is

<?xml version="1.0" standalone="yes" ?>
<root>
      <row>
              <MANUFACTURER>3Com</MANUFACTURER>
              <PRODUCT>Part1</PRODUCTN>
              <COUNT>2</COUNT>
        </row>
      <row>
              <MANUFACTURER>3Com</MANUFACTURER>
              <PRODUCT>Part2</PRODUCTN>
              <COUNT>1</COUNT>
        </row>
      <row>
              <PUBLISHER>Test Corp.</PUBLISHER>
              <PRODUCT>Book1</PRODUCT>
              <COUNT>1</COUNT>
        </row>
</root>

Here is the function I use to create XML document based on the XML Schema:

Public Sub WriteDataToXML(ByVal path As String, ByVal dt As System.Data.DataTable, Optional ByVal schemapath As String = "")
        Dim ds As DataSet
        Try
            ds = New DataSet("root")
            '' dt.TableName = "row"
            ds = New DataSet
            ds.Tables.Add(dt.Copy)

            If Not Trim(schemapath) = "" Then
                ds.InferXmlSchema(schemapath, Nothing)
            End If
            ds.WriteXml(path)
        Catch ex As Exception
            Console.Write("ERROR: " & ex.Message)
        Finally
            ds = Nothing
        End Try

    End Sub

and here is a sql statement I use to retrieve data:

SELECT MANUFACTURERS.MANUFACTURER,PRODUCT, COUNT(PRODUCT) As [COUNT]
FROM PRODUCTS
INNER JOIN MANUFACTURERS ON PRODUCTS.MAN_ID = MANUFACTURERS.MAN_ID
GROUP BY PRODUCTS.PRODUCT, MANUFACTURERS.MANUFACTURER
ORDER BY MANUFACTURERS.MANUFACTURER
0
Comment
Question by:YZlat
  • 8
  • 7
16 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17031067
DataSets get serialized in that format.  You might be able to use XSLT to get the XML that you want from the XML that you get from the WriteXml method call.

Bob
0
 
LVL 35

Author Comment

by:YZlat
ID: 17031209
is there a way to change my dataset to match the format specified in xsd?

what is the point of xsd?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17031300
Sounds like you are looking for an XSD tutorial:

http://www.w3schools.com/schema/default.asp

 - XML Schema is an XML-based alternative to DTDs.
 - An XML Schema describes the structure of an XML document.
 - The XML Schema language is also referred to as XML Schema Definition (XSD).

AFAIK It doesn't matter what XSD, you have defined, a DataSet gets serialized one way.

If you need a specific format, then you can implement your own writer, or transform the XML using XSLT to meet the specifications.

Bob

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Author Comment

by:YZlat
ID: 17031516
how can I implement my own writer? do you have any code examples?
0
 
LVL 35

Author Comment

by:YZlat
ID: 17031622
I can't utilize XSLT since I need to modify the structure of my XML and not just the way it is displayed
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17031654
XSLT can be used to alter the structure, it isn't just a display tool.
0
 
LVL 35

Author Comment

by:YZlat
ID: 17031716
could anyone help me with that?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17031943
What is the structure of the DataSet like?

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17031950
Are you saying that you aren't allowed to use XSLT, or that you don't know how to use XSLT?

Bob
0
 
LVL 35

Author Comment

by:YZlat
ID: 17032244
I know how to use XSLY for Web applications, but my function is a part of a component without user interface and XML controls.

my DataSet has three columns:

MANUFACTURERS.MANUFACTURER,PRODUCT, COUNT(PRODUCT) As [COUNT]
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1500 total points
ID: 17032782
Here is a possibility, but the DataSet needs 2 tables:

Unique MANUFACTURER -> MANUFACTURER, PRODUCT, COUNT

Imports System.Xml
Imports System.IO

Public Class DataSetWriter

  Private m_writer As XmlTextWriter

  Public Sub WriteXml(ByVal ds As DataSet, ByVal filePath As String)
    Dim stream As New FileStream(filePath, FileMode.Create)
    Me.WriteXml(ds, stream)
    stream.Close()
  End Sub

  Public Sub WriteXml(ByVal ds As DataSet, ByVal stream As Stream)
    m_writer = New XmlTextWriter(stream, System.Text.Encoding.UTF8)

    m_writer.WriteStartDocument(True)

    ' <root>
    m_writer.WriteStartElement("root")

    Me.WriteRecords(ds.Tables(0), ds.Tables(1))

    ' </root>
    m_writer.WriteEndElement()

    m_writer.WriteEndDocument()

  End Sub

  Private Sub WriteRecords(ByVal parent As DataTable, ByVal children As DataTable)

    For Each parentRow As DataRow In parent.Rows

      ' <MANUFACTURER>
      m_writer.WriteStartElement("MANUFACTURER")

      ' title
      m_writer.WriteAttributeString("title", parentRow("MANUFACTURER").ToString())

      For Each childRow As DataRow In parentRow.GetChildRows("parent_child")

        ' <PRODUCT>
        m_writer.WriteStartElement("PRODUCT")

        ' title
        m_writer.WriteAttributeString("title", childRow("PRODUCT").ToString())

        ' count
        m_writer.WriteAttributeString("count", childRow("COUNT").ToString())

        ' </PRODUCT>
        m_writer.WriteEndElement()

      Next childRow

      ' </MANUFACTURER>
      m_writer.WriteEndElement()

    Next parentRow

  End Sub

End Class

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17032796
The DataSet needs a relation between the parent table and the child table named 'parent_child'

Bob
0
 
LVL 35

Author Comment

by:YZlat
ID: 17053154
Bob, I get an error "a string literal was not closed" when trying to open my xml file, created with your code
0
 
LVL 35

Author Comment

by:YZlat
ID: 17053203
it seems that only 1027 characters are written to a stream, and that's why string literal is not closed

I get something like:

<PRODUCT title="Book1...
0
 
LVL 35

Author Comment

by:YZlat
ID: 17059484
Thanks Bob! I got it working. I deleted

 Public Sub WriteXml(ByVal ds As DataSet, ByVal filePath As String)
    Dim stream As New FileStream(filePath, FileMode.Create)
    Me.WriteXml(ds, stream)
    stream.Close()
  End Sub


and then changed

Public Sub WriteXml(ByVal ds As DataSet, ByVal stream As Stream)
    m_writer = New XmlTextWriter(stream, System.Text.Encoding.UTF8)

to

Public Sub WriteXml(ByVal ds As DataSet, ByVal filePath as string)
    m_writer = New XmlTextWriter(filePath, System.Text.Encoding.UTF8)

and now it creates the xml with no problem.

Thanks again!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17059555
Cool.  Ur velcome ;)

Bob
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question