Solved

XML Schema question

Posted on 2006-07-03
16
452 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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