提取XML节点块 [英] Extract Block of XML Nodes

查看:76
本文介绍了提取XML节点块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我打算将网格视图数据绑定到具有从xml字符串提取的数据的DataSet.

我检索了Xml字符串,并尝试使用XPathNavigator读取子树以提取要分配给数据集的节点.

但是,我没有这样做.当我运行代码时,子树总是带回整个Xml字符串,而不是要尝试提取的节点的所需块".

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




例如. Xml字符串




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.

推荐答案

大家好,

感谢您的关注...但是我设法解决了这个问题.

Hi Guys,

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

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



此链接对我有帮助...
http://www.dotnetjohn.com/articles.aspx?articleid=154 [ ^ ]



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


这篇关于提取XML节点块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆