C# 将根添加到 XDocument [英] C# Adding a root to an XDocument

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

问题描述

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

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