C#添加根一个XDocument [英] C# Adding a root to an XDocument

查看:420
本文介绍了C#添加根一个XDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含XML字符串,让我们这样说:

 <&小说GT; 
<图书>
<标题>猫在帽子< /标题>
<价格> 12< /价格>
< /书籍及GT;
< /小说>



我想打,看起来像这样一个XDocument:

 <&书目GT; 
<&小说GT;
<图书>
<标题>猫在帽子< /标题>
<价格> 12< /价格>
< /书籍及GT;
< /小说>
< /书目>



我可以加载使用的XDocument文档=的XDocument将XML字符串转换成一个XDocument。解析(的xmlString);



我将如何加载下一个新的根文件。我能想到的东西,如创建与我希望根新的XDocument,然后用一个for循环中添加节点作为孩子,但有这样做的更简单的方法?


解决方案

 的XDocument yourResult =新的XDocument(新的XElement(书目,doc.Root)); 


I have a string that contains an XML, lets say like this:

<Novels> 
 <Book>
  <Title>Cat in hat</Title>
  <Price>12</Price>
 </Book>
</Novels>

I want to make an XDocument that looks like this:

<Booklist>
 <Novels> 
  <Book>
   <Title>Cat in hat</Title>
   <Price>12</Price>
  </Book>
 </Novels>
</Booklist>

I can load the xml string into an XDocument using XDocument doc = XDocument.Parse(xmlString);

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆