Home > Xml > create and save xml document using c#

create and save xml document using c#

The code for creating and saving the xml document is described here..
is all about my experiments (from different sites you may find different approach,but the logic described here is feels good and easy for me)

Namespace you need to use is using System.Xml;

If you use this code
string xmlname = "Files/Form.xml"; //saves the xmlfile in this name in Files Folder OF Applciation make sure this folderis exists
XmlDocument doc = new XmlDocument(); //creates the instance of xmldocument
//For getting the xmlversion informations..not mandatory
XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
doc.AppendChild(declaration);//Append this node to the document object
//declare root of the xml document and append this to the document object
XmlElement mainRoot = doc.CreateElement("Root");
doc.AppendChild(mainRoot);
doc.Save(xmlname);

you get output as view output1 From image
If you use this code
string xmlname = "Files/Form.xml"; //saves the xmlfile in this name in Files Folder OF Applciation make sure this folderis exists
XmlDocument doc = new XmlDocument(); //creates the instance of xmldocument
//For getting the xmlversion informations..not mandatory
XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
doc.AppendChild(declaration);//Append this node to the document object
//declare root of the xml document and append this to the document object
XmlElement mainRoot = doc.CreateElement("Root");
doc.AppendChild(mainRoot);
XmlElement childnode = doc.CreateElement("Child1"); //it creates a child node
mainRoot.AppendChild(childnode); //Append childNode to Root OF xmlDocumeet

doc.Save(xmlname);

you get output as view output2 From image
output
If you use this code
XmlElement childnode = doc.CreateElement("Child1"); //it creates a child node string cdatavalue = "This Is Some text inside node"; //assign value needed inside this tag
XmlCDataSection subitem_value = doc.CreateCDataSection(cdatavalue); //create cdata object and store value to cdata object
childnode.AppendChild(subitem_value); //append cdata object to the childnode
mainRoot.AppendChild(childnode); //Append childNode to Root OF xmlDocumeet
//Save Xml Document
doc.Save(xmlname);

you get output as view output3 From image
If you use this code

childnode.InnerText = "This Is Some text inside node";
childnode.InnerXml = "This Is Some text inside node";

you get output as view output4 From image

If you use this code
XmlElement Nextchildnode = doc.CreateElement("Child1"); //it creates a child node
Nextchildnode.InnerXml = "This is second Node"; //we can use innertext also for norma text values
XmlAttribute Attribute = doc.CreateAttribute("Type"); //creates an attribute object
Attribute.Value = "Value Of Attribute"; //fill the value of attribute
Nextchildnode.Attributes.Append(Attribute); //append the attribute to the node
mainRoot.AppendChild(Nextchildnode); //Append childNode to Root OF xmlDocumeet

you get output as view output5 From image

Finaly if u use this section of code

XmlElement Nextchildnode = doc.CreateElement(“Child1”); //it creates a child node
XmlAttribute Attribute = doc.CreateAttribute(“Type”); //creates an attribute object
Attribute.Value = “Value Of Attribute”; //fill the value of attribute
Nextchildnode.Attributes.Append(Attribute); //append the attribute to the node
XmlElement subchilds = doc.CreateElement(“SubChild1”); //it creates a Subchild node
subchilds.InnerXml = “This is first Sub Node”; //we can use innertext also for norma text values
Nextchildnode.AppendChild(subchilds);//Append this node as the child OF Subchild
subchilds = doc.CreateElement(“SubChild2”); //it creates a 2nd Subchild node
subchilds.InnerXml = “This is Second Sub Node”; //we can use innertext also for norma text values
Nextchildnode.AppendChild(subchilds);//Append this node as the child OF Subchild
mainRoot.AppendChild(Nextchildnode); //Append childNode to Root OF xmlDocumeet
//Save Xml Document
doc.Save(xmlname);

You can view an output like this

you get output as view output6 From image

Advertisements
Categories: Xml
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: