Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
Hi,
 
I need to replace the 'DETAILS' node with 'ITEMDETAILS' in the below xml using c#.
I have tried with ReplaceChild method, but could not do that.
 
<?xml version="1.0" ?>
<Root>
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<H1>A</H1>
<H2>B</H2>
<H3>C</H3>
<H4>D</H4>
</Header>
<DETAILS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Item>
<Item1>I1</Item1>
<Item2>I2</Item2>
</Item>
<Item>
<Item1>I3</Item1>
<Item2>I4</Item2>
</Item>
</DETAILS>
</Root>
 
can someone please help me in this?
 
Thanks
Posted 17-Jun-11 1:44am
Comments
bhavani999 at 12-Feb-13 2:02am
   
Just Like the above example I want to replace the entire Values of Details node with new values and getting merged in older XML file.
Please help me how to do it?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you want to use XmlDocument, then:
 
var doc = new XmlDocument();
doc.LoadXml(" ... some xml ... ");
var root = doc.GetElementsByTagName("Root")[0];
var oldElem = root.SelectSingleNode("DETAILS");
var newElem = doc.CreateElement("ITEMDETAILS");
root.ReplaceChild(newElem, oldElem);
while (oldElem.ChildNodes.Count != 0)
{
    newElem.AppendChild(oldElem.ChildNodes[0]);
}
while (oldElem.Attributes.Count != 0)
{
    newElem.Attributes.Append(oldElem.Attributes[0]);
}
  Permalink  
Comments
monu_1983 at 17-Jun-11 7:37am
   
Thanks, It solved my problem
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

XElement xe = XElement.Load("XML FILE PATH");
            XElement oldnode = xe.Element("DETAILS");
            XElement newnode = new XElement("ITEMDETAILS", oldnode.Elements());
            foreach (var item in oldnode.Attributes())
            {
                newnode.SetAttributeValue(item.Name, item.Value);
            }
            xe.Add(newnode);
            oldnode.Remove();
  Permalink  
Comments
monu_1983 at 17-Jun-11 7:29am
   
Thanks for your answer.
Is it possible to do this using XMLDocument? because I am doing some other operations with this xml using xmldocument and would like to have my xml in a single object (if possible)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

ReplaceChild works fine. Without your code (or an explanation of what error you are getting) it is hard to know what is going wrong for you but check the following:
 
1. You have the parameters the right way round, it is ReplaceChild(new, old)
 
2. The new node must be created from the same document
 
3. The replace child must be done on an ancestor of the old node
 
One thing is that if you want to rename DETAILS to ITEMDETAILS then you need to copy all the attributes and child nodes across to the new node, otherwise you will get the following:
 
<?xml version="1.0"?>
<Root>
  <Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <H1>A</H1>
    <H2>B</H2>
    <H3>C</H3>
    <H4>D</H4>
  </Header>
  <ITEMDETAILS />
</Root&gt;
 
Which I suspect is not what you want. What you need to do is copy each child element and attribute to the new node, as shown here[^]
  Permalink  
Comments
monu_1983 at 17-Jun-11 8:37am
   
Thanks for the explanation, I was getting below error when I was trying with append child method, "The node to be removed is not a child of this node".
I will try with your suggestion again.
Graham Shanks at 17-Jun-11 14:59pm
   
Try using the DocumentElement property of the XmlDocument - that will work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 17 Jun 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100