通过XElement的属性值获取 [英] Get XElement by value of its attribute

查看:46
本文介绍了通过XElement的属性值获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML:

<rootNode>
  ... some stuff
  <ReportCellRef>
    <dang n="DVCompany" h="0" u="0" o="0" fmt="0">
      ... some stuff
    </dang>
  </ReportCellRef>
</rootNode>

我想得到< dang ...>...</dang> 节点作为XElement,因此只要具有 n 属性的值,我就可以将其替换为另一个节点.

And I want get the <dang ...> ... </dang> node as XElement, so I can replace it with another node, provided I have the value of the n attribute.

我有以下代码:

Dim nameToSearch = importNode.Attribute("n").Value
Dim replaceable = From dangToTake In xdoc.Elements("ReportCellRef") _
                  Where CStr(dangToTake.Element("dang").Attribute("n")) = nameToSearch
                  Select dangToTake

For Each nodeToReplace As XElement In replaceable
    nodeToReplace.ReplaceWith(importNode)
Next nodeToReplace

但是LINQ查询不会产生任何结果...有什么想法吗?

But the LINQ query does not yield any results... Any ideas?

推荐答案

在其中抛出"Descendants()"调用:

Throw a "Descendants()" call in there:

dim xdoc as XDocument = XDocument.Parse("<rootNode><ReportCellRef><dang n=""DVCompany"" h=""0"" u=""0"" o=""0"" fmt=""0""></dang></ReportCellRef></rootNode>")
Dim replaceable = From dangToTake In xdoc.Descendants().Elements("ReportCellRef") _
              Where dangToTake.Element("dang").Attribute("n").Value = "DVCompany"
              Select dangToTake

这篇关于通过XElement的属性值获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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