2015-02-23 80 views
0

我有下面的XML:获取的XElement其属性的值

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

我想获得<dang ...> ... </dang>节点的XElement,这样我就可以与另一个节点代替它,提供我的价值n属性。

我有这样的代码:

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查询没有任何结果...任何想法?

回答

0

抛出一个 “后裔()” 调用有:

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 
1

您正在比较XAttribute和它的值。 CStr(dangToTake.Element("dang").Attribute("n"))不会为您提供该属性的值。改为尝试dangToTake.Element("dang").Attribute("n").Value

+0

仍然是空的查询结果... – Syspect 2015-02-23 14:30:13