我有一个看起来像这样的XML文档:
<root>
Maybe some text
<thing>thing can have text</thing>
<thing>it can even be on multiple
lines
</thing>
<thing>a third thing</thing>
This text resets the numbering
<thing>this thing is not part of the above list and should have number 1</thing>
<some-element-not-thing>Also resets numbering</some-element-not-thing>
<thing>this thing should also have number 1<thing/>
</root>
我需要在<thing>
连续出现时给它们编号,给每个名为&#34; number&#34;的属性。也就是说,我期望的结果是:
<root>
Maybe some text
<thing number="1">thing can have text</thing>
<thing number="2">it can even be on multiple
lines
</thing>
<thing number="3">a third thing</thing>
This text resets the numbering
<thing number="1">this thing is not part of the above list and should have number 1</thing>
<some-element-not-thing>Also resets numbering</some-element-not-thing>
<thing number="1">this thing should also have number 1<thing/>
</root>
我将如何处理这样的事情?我无法在XmlDocument中找到在元素之间查找文本的方法(但它确实允许我按顺序枚举元素,所以当我遇到不是<thing>
的东西时我可以重置编号,而我不是确保LINQ to XML允许我在元素之间获取文本,因为它只会产生元素或后代,它们都不代表&#34;松散文本&#34;。
也许这个&#34;松散的文字&#34;是坏的(但显然是可解析的)XML?
答案 0 :(得分:1)
与往常一样,如果您在提出问题之前提供了您已经尝试过的内容,那将会很有帮助。有很多关于解析和操作XML的博客文章和问题。
首先,我将使用LINQ to XML进行解析。然后,您所要做的就是遍历根元素下面的节点,为每个thing
元素分配一个递增的数字。当下一个元素不是thing
而不是空格时,此计数器将被重置:
var doc = XDocument.Parse(xml, LoadOptions.PreserveWhitespace);
var i = 0;
foreach (var node in doc.Root.Nodes())
{
var element = node as XElement;
var text = node as XText;
var isThing = element != null && element.Name == "thing";
var isWhitespace = text != null && string.IsNullOrWhiteSpace(text.Value);
if (isThing)
{
element.Add(new XAttribute("number", ++i));
}
else if (!isWhitespace)
{
i = 0;
}
}
var result = doc.ToString();