Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change XML root element name

Tags:

c#

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?

like image 571
Pradeep Avatar asked Aug 30 '10 14:08

Pradeep


People also ask

How do you define a root element in XML?

Each XML document has exactly one single root element. It encloses all the other elements and is, therefore, the sole parent element to all the other elements. ROOT elements are also called document elements. In HTML, the root element is the <html> element.

Can XML have 2 root elements?

While a properly formed XML file can only have a single root element, an XSD or DTD file can contain multiple roots. If one of the roots matches that in the XML source file, that root element is used, otherwise you need to select one to use.

Does XML need a root element?

An XML document must have a single root element, which contains all other XML elements in the document.

Does XML document have root tag?

Android "Valid XML document must have a root tag at line"


1 Answers

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;
like image 138
Will A Avatar answered Sep 28 '22 02:09

Will A