Linq to XML & ASP.Net - part 6
This is the sixth post in a new series of posts regarding LINQ to XML and ASP.Net.Please do read the first post here. You can find the second post here .
You can find the third post here . You can read the fourth post here . You can find the fifth post here .In this post I would like to show on how to create a xml document on the fly and save it on a file in our local disk.
will use Visual Studio 2010 Ultimate edition and C# as the development
language. Visual Studio 2010/2008 express editions will suffice.
1) Fire VS and create a web site. Choose an appropriate name for your site.
2) In the code behind file Default.aspx.cs, add the namespace
All the classes we need to manipulate XML documents are inside this namespace.
In this sixth example we want to find the the information of the second element.
3) In the Page_Load event handling routine of the default.aspx.cs file, type the following
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement( "Students",
new XComment("We only have 3 elements"),
new XElement("StudentId", "5"),
new XElement("Name", "Lewis"),
new XElement("Sex", "Male")
We use the XDocument class to represent an xml document. Then we create a new XML declaration (XDeclaration) and after that we do create XML elements (XElement) with the appropriate content.
Finally we use the Save method of the XDocument object, to save the newly created file in the local filesystem.
Email me if you need the source code. Hope its helps !!!!