Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys,

I am attemtping to to databind a gridview to a DataSet which has data extracted from an xml string.

I retrieve the Xml string and am attempting to use the XPathNavigator to read the subtree to extract the nodes that i want to assign to the dataset.

I am however failing to do so. When i run the code, the subtree always brings back the entire Xml string instead of the desired 'block' of Nodes i am trying to extract.

<pre lang="vb">Dim xmlVal = "<xml content here>"
          Dim dsHistoryData As New DataSet()

          Dim xmlSR As System.IO.StringReader = New System.IO.StringReader(xmlVal)
          Dim document As System.Xml.XPath.XPathDocument = New System.Xml.XPath.XPathDocument(xmlSR)
          Dim navigator As System.Xml.XPath.XPathNavigator = document.CreateNavigator()

          Dim house As System.Xml.XmlReader = navigator.ReadSubtree()
          Dim myTest As String

          'Stream the book element and its child nodes to the XmlReader.
          navigator.MoveToChild(&quot;HouseHistory&quot;, &quot;&quot;)
          navigator.MoveToChild(&quot;House&quot;, &quot;&quot;)
  

          While book.Read
              myTest += house.ReadInnerXml() + "<br>"
          End While

          Response.Write(myTest)
          Response.End()



eg. of Xml string
<pre lang="xml">
<HouseInputData>
    <HouseAddedBy>Joe Soap</HouseAddedBy>
</HouseInputData>
<HouseOutputData>
    <HouseAddress>99318</HouseAddress>
    <History>
        <HouseHistory>
            <House>
                <OwnerTitleCode>Mr      </OwnerTitleCode>
                <OwnerInitials>MH</OwnerInitials>
                <OwnerSurname>ANA</OwnerSurname>
                <OwnerFirstName>HENDERSON</OwnerFirstName>
            </House>
        </HouseHistory>
    </History>
</HouseOutputData>



Any help would be greatly appreciated.
Posted
Updated 23-May-11 21:36pm
v2

1 solution

Hi Guys,

thanks for having a look...i have however managed to solve this.

VB
Dim xmlVal = oXml.ReturnXML
            Dim dsHistoryData As New DataSet()

            Dim xmlSR As System.IO.StringReader = New System.IO.StringReader(xmlVal)
            Dim doc As System.Xml.XPath.XPathDocument
            doc = New System.Xml.XPath.XPathDocument(xmlSR)
            Dim nav As System.Xml.XPath.XPathNavigator
            nav = doc.CreateNavigator()
            Dim expr As System.Xml.XPath.XPathExpression = nav.Compile("//HouseHistory/*")
            Dim nodes As System.Xml.XPath.XPathNodeIterator = nav.Select(expr)
            While (nodes.MoveNext())
                Response.Write(nodes.Current.OuterXml())

            End While

            Response.End()


This link helped me...
http://www.dotnetjohn.com/articles.aspx?articleid=154[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900