【问题标题】:read specific element from the xml string?从 xml 字符串中读取特定元素?
【发布时间】:2014-08-23 08:49:59
【问题描述】:

我有一个xml字符串存储在数据库中

 <?xml version="1.0" encoding="utf-16" standalone="yes"?>
<Table>
  <Product>
    <Product_id>1</Product_id>
    <Product_name>Product 1</Product_name>
    <Product_price>1000</Product_price>
  </Product>
  <Product>
    <Product_id>2</Product_id>
    <Product_name>Product 2</Product_name>
    <Product_price>2000</Product_price>
  </Product>
  <Product>
    <Product_id>3</Product_id>
    <Product_name>Product 3</Product_name>
    <Product_price>3000</Product_price>
  </Product>
  <Product>
    <Product_id>4</Product_id>
    <Product_name>Product 4</Product_name>
    <Product_price>4000</Product_price>
  </Product>
</Table>

  Dim reader As XmlReader = XmlReader.Create(New StringReader(xmlstring)) 

其中 xmlstring 是 String 包含上述 xml 数据。 如何直接读取 id 4 的产品?

【问题讨论】:

  • 你可以使用 reader.read 方法..
  • 不,我不想用这个。因为每次我都必须循环获取元素,这是我最讨厌的。是否有任何直接功能可以转到特定 id 的元素?
  • 这是数据集 XML 吗?

标签: .net xml vb.net winforms


【解决方案1】:

您可以使用 XDocumentXDocument.Parse() 方法轻松完成此操作,而无需手动遍历 XML 元素:

Dim doc = XDocument.Parse("your xml string here")
Dim product = doc.Root _
                 .Elements("Product") _
                 .FirstOrDefault(Function(x) x.Element("Product_id").Value = "4")
'at this point, product contain the desired <Product> element'
'you can get product name this way :'
Dim productName = product.Element("Product_name").Value

更新:

由于 XDocument 在 .NET 2.0 中不可用,我们必须迁移到较旧的 XmlDocument。对于那些熟悉 XPath 的人来说,这甚至可以更简单:

Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(xmlstring)

Dim productName = doc.SelectSingleNode("/Table/Product[Product_id='4']/Product_name") _
                     .InnerText

上面示例中使用的 XPath 意思是,搜索具有子节点 &lt;Product_id&gt; 等于 4 的节点 &lt;Product&gt;,然后从该 &lt;Product&gt; 得到 Product_name&gt; 子节点

【讨论】:

  • 它仅适用于 dot net framework 3.5 及更高版本。它在我的应用程序中不起作用,因为它在 Framework 2.0 中,我无法更改为 3.5 :(
  • 我可以在framework 2.0的应用中达到同样的效果吗?
【解决方案2】:

如果您不使用循环,那么我认为最好改用XmlDocument 类。用LINQ 给它加点趣味,问题就解决了。

Dim document As XmlDocument = New XmlDocument()

document.LoadXml(xmlstring)

Dim match As XmlNode = (
    From
        node As XmlNode
    In
        document.SelectNodes("/Table/Product").Cast(Of XmlNode)()
    Where
        node.SelectNodes("Product_id")(0).InnerText = "4"
    Select
        node
).FirstOrDefault()

If (match Is Nothing) Then
    'No match
Else
    'Found
End If

.Net 2.0

使用For Each 循环:

Dim document As XmlDocument = New XmlDocument()

document.LoadXml(xmlstring)

Dim match As XmlNode = Nothing

For Each node As XmlNode In document.SelectNodes("/Table/Product")
    If (node.SelectNodes("Product_id")(0).InnerText = "4") Then
        match = node
        Exit For
    End If
Next

If (match Is Nothing) Then
    'No match
Else
    'Found
End If

【讨论】:

  • 它显示错误 'Cast is not a member of System.Xml.XmlNodeList' 。在框架 2.0 中有什么方法可以做到这一点吗?
  • @ErMayank 你是对的,这不适用于.Net 2.0。请查看我的编辑。
猜你喜欢
  • 2021-03-30
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2022-11-10
  • 1970-01-01
相关资源
最近更新 更多