Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XML Schema question

Posted on 2006-07-03
16
Medium Priority
?
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

604 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