9
votes

I have XML stored in string variable:

<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>

Here I want to change XML tag <ItemMasterList> to <Masterlist>. How can I do this?

5
You should post a bit of the code you are using, as there is more than one way to do this. - Robert Harvey
+ 1 for the question. I was going to bet a fortune that such question did not exist here. Very unusual scenario. - antanta

5 Answers

13
votes

System.Xml.XmlDocument and the associated classes in that same namespace will prove invaluable to you here.

XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("MasterList");
docNew.AppendChild(newRoot);
newRoot.InnerXml = doc.DocumentElement.InnerXml;
String xml = docNew.OuterXml;
9
votes

I know i am a bit late, but just have to add this answer as no one seems to know about this.

XDocument doc = XDocument.Parse("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");    
doc.Root.Name = "MasterList";

Which returns the following:

<MasterList>
  <ItemMaster>
    <fpartno>xxx</fpartno>
    <frev>000</frev>
    <fac>Default</fac>
  </ItemMaster>
</MasterList>
7
votes

You can use LINQ to XML to parse the XML string, create a new root and add the child elements and attributes of the original root to the new root:

XDocument doc = XDocument.Parse("<ItemMasterList>...</ItemMasterList>");

XDocument result = new XDocument(
    new XElement("Masterlist", doc.Root.Attributes(), doc.Root.Nodes()));
0
votes

Using the XmlDocument way, you can do this as follows (and keep the tree intact):

XmlDocument oldDoc = new XmlDocument();
oldDoc.LoadXml("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
XmlNode node = oldDoc.SelectSingleNode("ItemMasterList");

XmlDocument newDoc = new XmlDocument();
XmlElement ele = newDoc.CreateElement("MasterList");
ele.InnerXml = node.InnerXml;

If you now use ele.OuterXml is will return: (you you just need the string, otherwise use XmlDocument.AppendChild(ele) and you will be able to use the XmlDocument object some more)

<MasterList>
  <ItemMaster>
     <fpartno>xxx</fpartno>
     <frev>000</frev>
     <fac>Default</fac>
  </ItemMaster>
</MasterList>
0
votes

As pointed by Will A, we can do it that way but for case where InnerXml equals the OuterXml the following solution will work out:

// Create a new Xml doc object with root node as "NewRootNode" and 
// copy the inner content from old doc object using the LastChild.
                    XmlDocument docNew = new XmlDocument();
                    XmlElement newRoot = docNew.CreateElement("NewRootNode");
                    docNew.AppendChild(newRoot);
// The below line solves the InnerXml equals the OuterXml Problem
                    newRoot.InnerXml = oldDoc.LastChild.InnerXml;
                    string xmlText = docNew.OuterXml;