是一排我有這個簡單的XMLC#:如何知道如果這兩個元素使用XML LINQ
<parent>
<child>
<son attribute="1"/>
<sp>
<son attribute="2"/>
<sp>
</child>
<child>
<another child>
<son attribute ="3"/>
<sp>
</another child>
</child>
<child>
<son attribute="5"/>
</child>
</parent>
這裏是我的代碼現在。其實我不知道接下來會發生什麼
XDocument doc = XDocument.Parse(string);
foreach (var el in doc.Descendants("child").ToList())
{
}
所以輸出將
<parent>
<child>
<son attribute="1"/>
<sp value="1">
<son attribute="2"/>
<sp value="2">
</child>
<child>
<another child>
<son attribute ="3"/>
<sp value="3">
</another child>
</child>
<child>
<son attribute="5"/>
</child>
</parent>
我的問題是,我如何檢查son
和sp
標籤是彼此相鄰如果相鄰,該sp
標籤將得到son
標籤
你可能會更好嘗試使用XSLT這份工作,而不是直的C#。我有一段時間沒有使用XSLT,但C#肯定支持它,這些SO問題可能有幫助:http://stackoverflow.com/questions/23274312/how-to-match-and-wrap-identical-and-相鄰節點在一起在xslt-1-0和http://stackoverflow.com/questions/32701706/xslt-identify-consecutive-nodes-which-has-same-patters-of-attribute-values – user783836
如果它不是兒子旁邊? – DarkKnight
@DarkKnight沒有任何反應,例如 – imadammy