string xmlNewData = @"
<ItemList>
<item1/>
<item3/>
</ItemList>
";
File.WriteAllText("tempData.xml", xmlNewData);
XElement element = XElement.Load("tempData.xml", LoadOptions.SetLineInfo);
element.Element("item1").AddAfterSelf(new XElement("item2"));
element.Element("item3").AddAfterSelf(new XElement("item4"));
element.Element("item4").AddAfterSelf(new XElement("item5"));
Console.WriteLine("new XML");
Console.WriteLine(element);
foreach (XElement item in element.DescendantsAndSelf())
{
if (((IXmlLineInfo)item).HasLineInfo())
{
Console.Write("Line{0} :\t",((IXmlLineInfo) item).LineNumber);
}
else
{
Console.Write("LineXX:\t");
}
Console.WriteLine(item.Name);
}
Linq to XML之添加结点
最新推荐文章于 2021-04-28 09:06:08 发布