Solved

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

Posted on 2010-09-05
2
350 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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