Results 1 to 3 of 3

Thread: Write XML using C#.NET

  1. #1

    Default Write XML using C#.NET

    This tutorial will show you how to write xml documents with C#.NET using the XmlTextWriter class from the System.Xml namespace. To view the article on how to read xml documents using C#.NET, click here

    Note: Remember to set the “write” permissions for your webserver on the directory where you like to write the XML document to.

    The XML classes in the System.Xml namespace that can be used to write XML documents are:

    • XmlWriter
    • XmlTextWriter


    XmlTextWriter is inherited from XmlWriter which means (in short) XmlWriter has a couple of extra features. But for the purpose of this tutorial and in most cases, you’ll only need to use XmlTextWriter.

    I’ve added comments to the lines of code below to explain what they do:

    Code:
    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Xml" %>
    <script runat="server">
    public void Page_Load(object Src, EventArgs E)
    {
    
    	//*** Written by user CWA, CoolWebAwards.com Forums. 18 March 2010
    	//*** http://forum.coolwebawards.com/
    	
    	// *** XmlTextWriter
    	XmlTextWriter myXmlWriter = new XmlTextWriter(Server.MapPath("test2.xml"), null);
    	myXmlWriter.Formatting = Formatting.Indented;
    	myXmlWriter.Indentation = 4;
    	
    	// *** Uncomment to use XmlWriter for extra settings if necessary. Comment the 3 lines above
    	//Dim myXmlSettings As New XmlWriterSettings
    	//myXmlSettings.Indent = True
    	//Dim myXmlWriter As XmlWriter = XmlWriter.Create(server.MapPath("test.xml"), myXmlSettings)
    	* * 
    	myXmlWriter.WriteComment("http://forum.coolwebawards.com");// <!--http://forum.coolwebawards.com-->
    	myXmlWriter.WriteStartElement("Products");// <Products>
    	
    	int i = 0;
    	for (i = 1; i <= 3; i++) {
    		//http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter_members.aspx
    		myXmlWriter.WriteStartElement("Product");// see next line
    		myXmlWriter.WriteAttributeString("Id", i.ToString());// <Product id="">
    		myXmlWriter.WriteElementString("price", "price element text for ID " + i.ToString());// <price>price element text for ID 1</price>
    		myXmlWriter.WriteElementString("weight", "weight element text for ID " + i.ToString());// <weight>price element text for ID 1</price>
    		myXmlWriter.WriteRaw(Environment.NewLine + "        <description>description element text for ID " + i.ToString() + "</description>" + Environment.NewLine);// <description>description element text for ID 3</description> | Just an example of how to write raw data
    		myXmlWriter.WriteEndElement();//</Product>
    	}
    	* * * * 
    	myXmlWriter.WriteEndElement();// </Products>
    	myXmlWriter.Close();
    
    }
    </script>
    The code above will produce the following xml document called test.xml:

    Code:
    <!--http://forum.coolwebawards.com-->
    <Products>
        <Product Id="1">
            <price>price element text for ID 1</price>
            <weight>weight element text for ID 1</weight>
            <description>description element text for ID 1</description>
    	</Product>
        <Product Id="2">
            <price>price element text for ID 2</price>
            <weight>weight element text for ID 2</weight>
            <description>description element text for ID 2</description>
    	</Product>
        <Product Id="3">
            <price>price element text for ID 3</price>
            <weight>weight element text for ID 3</weight>
            <description>description element text for ID 3</description>
    	</Product>
    </Products>
    Click here for more information on how to read an xml document using C#.NET
    CWA - CoolWebAwards.com

  2. #2
    Emerald Star member
    Join Date
    Sep 2011
    Posts
    108

    Default Write XML using C#.NET

    XmlWriter class contains the functionality to write to XML documents. It is an abstract base class used through XmlTextWriter and XmlNodeWriter classes. It contains methods and properties to write to XML documents. This class has several Writexxx method to write every type of item of an XML document. For example, WriteNode, WriteString, WriteAttributes, WriteStartElement, and WriteEndElement are some of them. Some of these methods are used in a start and end pair. For example, to write an element, you need to call WriteStartElement then write a string followed by Write End Element.

  3. #3

    Default

    I dont like the w3 schools material, does not seem very detailed to me.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •