How to Copy all Child Nodes(innerXML) to another Xelement

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Dear all,
I have XML Like
<pre class="prettyprint" style=" <Server Name="Server1" ID="601" Size="A" >
<ConfiguredNode></ConfiguredNode>
<Node Number="1 </Node>
<ModelDetails></ModelDetails>
<DataSaved>True</DataSaved>
<---> .... </-->
</Server>

<Server Name="Server2" ID="602" Size="A" >

</Server>

<Server Name="Server3" ID="603" Size="A" >

</Server>

<Server Name="Server4" ID="604" Size="A" >

</Server>

<Server Name="Server5" ID="605" Size="A" >

</Server>[/code]
<br/>
I have to copy all the Child nodes under first Server to all the bellow servers using XDocument and Xelement.
<pre class="prettyprint" style=" string innerxml=string.empty; foreach (var xserver in xServerDoc.Descendants("Server"))
{
if (xserver.Name.LocalName.Contains("Server1"))
{
StringBuilder sb = new StringBuilder();
foreach (var node in xserver.Nodes())
{
sb.Append(node.ToString());
}
innerxml=sb.tostring();
}
}

foreach (var xserver in xServerDoc.Descendants("Server"))
{
if (!xserver.Name.LocalName.Contains("Server1"))
{
xserver.Add(XElement.Parse(innerXml));
}
} [/code]
<br/>
<br/>
here iam getting the exception like " There are multiple root elements ".
I am breaking my head from past 4 hours...I am not getting any solution to fix.
Please help me to fix this issue.....
Thanks in Advance
Pavan Kumar B.V
<br/>
<br/>
<br/>

View the full article
 
Back
Top