Solved

Can't load Dataset with XML and Schema from another Dataset

Posted on 2010-09-05
2
342 Views
Last Modified: 2013-11-07
I'm trying to pass a dataset as xml from my web service to my web page. My example code is below and gives me a "Root element is missing" error.

Here's my code behind:
' This is actually a web service method
Public Function GetDatasetXml(ByRef sXmlSchema As String) As String
    Dim sSQL As String = "SELECT ProductId, Name, Color FROM Product WHERE Color IS NULL"
    Dim ds As New Dataset = DbHelper.OpenDataset(sSQL)

     sXmlSchema = ds.GetXmlSchema()

     return ds.GetXml()
End Function

Public Sub BindMarkup
    Dim sXMLSchema As String = String.Empty
    Dim sXML As String = GetDatasetXml(sXMLSchema)

    Dim readerXMLSchema As XmlReader = XmlReader.Create(New StringReader(sXMLSchema))
    Dim readerXML As New System.IO.StringReader(sXML)

    Dim ds As New DataSet
    ds.ReadXmlSchema(readerXML)
    ds.ReadXml(readerXML, XmlReadMode.ReadSchema)

    Me.rptList.DataSource = ds
    Me.rptList.DataBind()
End Sub

Open in new window

and my markup:
      <asp:Repeater ID="rptList" runat="server" >
        <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "ProductID")%><br>
             &nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "Name")%><br>
             &nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "Color")%><br>
        </ItemTemplate>
      </asp:Repeater>

Open in new window


I tried this originally without using the XML Schema but that failed because the Color field is null and caused the repeater to complain the Color field wasn't found. So I know I need the schema but I can't figure out how to get this to work.

Thanks in advance.
0
Comment
Question by:ZekeLA
  • 2
2 Comments
 
LVL 1

Author Comment

by:ZekeLA
ID: 33606812
A developer friend of mine gave me the solution. I'll post it if it's not proprietary.
0
 
LVL 1

Accepted Solution

by:
ZekeLA earned 0 total points
ID: 33799799
Here is the routine to create a dataset from the xml and xmlschema.
    Public Function XMLToDataSet( _

            ByVal sXml As String, _

            Optional ByVal sXmlSchema As String = "")



        ' Process XML

        Dim sr As StringReader = New StringReader(sXml)

        Dim xtr As XmlTextReader = New XmlTextReader(sr)



        ' Process Schema

        Dim srSchema As StringReader = Nothing

        Dim xtrSchema As XmlTextReader = Nothing

        If sXmlSchema.Length > 0 Then

            srSchema = New StringReader(sXmlSchema)

            xtrSchema = New XmlTextReader(srSchema)

        End If



        Dim ds As New DataSet

        If sXmlSchema.Length > 0 Then

            ds.ReadXmlSchema(xtrSchema)

        End If

        ds.ReadXml(xtr)



        Return ds

    End Function

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

13 Experts available now in Live!

Get 1:1 Help Now