2015-02-23 76 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