Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向具有C中现有根节点的Xml文档中添加根节点#_C#_Xml - Fatal编程技术网

C# 向具有C中现有根节点的Xml文档中添加根节点#

C# 向具有C中现有根节点的Xml文档中添加根节点#,c#,xml,C#,Xml,我有一个xml文档,可能看起来像这样 <Stuff> <SomeStuff></SomeStuff> </Stuff> <Root> <Stuff> <SomeStuff></SomeStuff> </Stuff> </Root> 我想给这个文档添加一个新的根,使它看起来像这样 <Stuff> <SomeStuff></SomeStuff&

我有一个xml文档,可能看起来像这样

<Stuff>
<SomeStuff></SomeStuff>
</Stuff>
<Root>
<Stuff>
<SomeStuff></SomeStuff>
</Stuff>
</Root>

我想给这个文档添加一个新的根,使它看起来像这样

<Stuff>
<SomeStuff></SomeStuff>
</Stuff>
<Root>
<Stuff>
<SomeStuff></SomeStuff>
</Stuff>
</Root>

这就是我试过的

string inputXml = " <Stuff>
    <SomeStuff></SomeStuff>
    </Stuff>";
 XmlDocument firstLossRootNode = new XmlDocument();
 firstLossRootNode.LoadXml("<Root />");
var economyDocument = = XDocument.Parse(inputXml);
firstLossRootNode.DocumentElement.AppendChild(economyDocument.Document);
string inputXml=”
";
XmlDocument firstLossRootNode=新的XmlDocument();
firstLossRootNode.LoadXml(“”);
var economyDocument==XDocument.Parse(inputXml);
firstLossRootNode.DocumentElement.AppendChild(economyDocument.Document);
我试图附加它有一个孩子,但我得到编译时错误
在c#中是否有方法使用Xdocument类实现这一点。

在您的示例中,您将XmlDocument类用于firstLossRootNode,而Xdocument类用于economyDocument。这是故意的吗?如果没有,以下代码将执行您尝试执行的操作:

using System;
using System.Xml.Linq;

string inputXml = "<Stuff><SomeStuff></SomeStuff></Stuff>";
XDocument firstLossRootNode = XDocument.Parse("<Root />");
XDocument economyDocument = XDocument.Parse(inputXml);

firstLossRootNode.Root.Add(economyDocument.FirstNode);
使用系统;
使用System.Xml.Linq;
字符串inputXml=“”;
XDocument firstLossRootNode=XDocument.Parse(“”);
XDocument economyDocument=XDocument.Parse(inputXml);
firstLossRootNode.Root.Add(economyDocument.FirstNode);

是的,有办法。你试过什么吗?当您这样做时,代码是什么样子的?它做了什么?这和你想让它做的有什么不同?