How To Create An XML Document In C#

Posted By Venu Thomas

It is very easy, as there I wondered, here is the code:

?View Code CSHARP
using System.Xml.Linq
protected void CreateDocumentXML ()
{
XDocument TheCodersXML = new XDocument(
new XDeclaration("1.0","utf-8","yes")
new XComment("List of Students")
new XElement("Students"
new XElement("Student"
new XAttribute("Id","No:123")
new XElement("Name","Diana Wallwalker")
new XElement("Age","23")),
 
new XElement("Student"
new XAttribute("Id","No:456")
new XElement("Name","Danny Thomas")
new XElement("Age","27")),
 
new XElement("Student"
new XAttribute("Id","No:789")
new XElement("Name","Roger Benny")
new XElement("Age","20")),
 
new XElement("Student"
new XAttribute("Id","No:012")
new XElement("Name","Venu Thomas"),
new XElement("Age","26"))
)
);
}

In the end it saved in the address you want:

?View Code CSHARP
TheCodersXML.Save(@"c:\TheCodersXML.xml");

 

 Output:

<?xml version="1.0″ encoding="utf-8″ standalone="yes"?>
<!-- List of Students -->
<students>
	<student Id="No:123">
		<name>Diana Wallwalker</name>
		<age>23</age>
	</student>
	<student Id="No:456">
		<name>Danny Thomas</name>
		<age>27</age>
	</student>
	<student Id="No:789">
		<name>Roger Benny</name>
		<age>20</age>
	</student>
	<student Id="No:012">
		<name>Venu Thomas</name>
		<age>26</age>
	</student>
</students>

Tags: , ,

  •  
  • Share with others:

    Thanks for visiting us! If you enjoyed these icons please feel free to share them! Or if you want to know what's going on with WiseCodes.com, follow us!

    Leave a Reply

    Anti-Spam Protection by WP-SpamFree

    Locations of visitors to this page eXTReMe Tracker