Solved

XML Schema question

Posted on 2006-07-03
16
442 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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)

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now