Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
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
<?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
Comments
bhavani999 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?

If you want to use XmlDocument, then:

C#
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]);
}
 
Share this answer
 
Comments
maheshk_blr 17-Jun-11 7:37am    
Thanks, It solved my problem
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();
 
Share this answer
 
Comments
maheshk_blr 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)
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
<?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[^]
 
Share this answer
 
Comments
maheshk_blr 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900