我有一个包含 XML 的字符串,可以这样说:
I have a string that contains an XML, lets say like this:
<Novels>
<Book>
<Title>Cat in hat</Title>
<Price>12</Price>
</Book>
</Novels>
我想制作一个如下所示的 XDocument:
I want to make an XDocument that looks like this:
<Booklist>
<Novels>
<Book>
<Title>Cat in hat</Title>
<Price>12</Price>
</Book>
</Novels>
</Booklist>
我可以使用 XDocument doc = XDocument.Parse(xmlString);
如何在新的根目录下加载文档.我可以考虑使用我想要的根创建一个新的 XDocument,然后使用 for 循环将节点添加为子节点,但是有更简单的方法吗?
How would I load the document under a new root. I can think of something like creating a new XDocument with the root I want and then using a for loop to add the nodes as children, but is there an easier way of doing this?
XDocument yourResult = new XDocument(new XElement("Booklist", doc.Root));
这篇关于C# 将根添加到 XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!