是一排我有这个简单的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