Tuesday, September 7th, 2010

write XML using XmlTextWriter in csharp

using System;
using System.IO;
using System.Text;
using System.Xml;
 
public class WriteXml {
  public static void Main(string [ ] args) {
 
    // Create the XmlWriter
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
 
    // Set the formatting to something nice
    writer.Formatting = Formatting.Indented;
 
    // Write the XML declaration
    writer.WriteStartDocument(true);
 
    // Write a comment
    writer.WriteComment("the first element follows.");
 
    // Start the root element
    writer.WriteStartElement("root");
 
    // Write an attribute
    writer.WriteAttributeString("id","_1");
 
    // Write another attribute
    writer.WriteStartAttribute"name", "foo");
    writer.WriteString("bar");
    writer.WriteEndAttribute( );
 
    // Write another element
    writer.WriteElementString("element1","some characters");
 
    writer.WriteStartElement("cdataElement");
    writer.WriteAttributeString("date",DateTime.Now.ToString( ));
    writer.WriteCData("<this contains some characters XML wouldn't like & would choke on");
    writer.WriteString("<this contains some characters XML wouldn't like & so the XmlWriter replaces them");
    writer.WriteEndElement( );
 
    // Write an empty element
    writer.WriteStartElement("emptyElement");
    writer.WriteEndElement( );
 
    // Write another empty element
    writer.WriteStartElement("emptyElement","foo");
    writer.WriteFullEndElement( );
 
    // Write some text
    writer.WriteString("One string ");
    writer.WriteEntityRef("amp");
    writer.WriteString(" another.");
 
    // Close the root element
    writer.WriteEndElement( );
 
    // End the document
    writer.WriteEndDocument( );
 
    // Flush and close the output stream
    writer.Flush( );
    writer.Close( );
  }
}
<?xml version="1.0" encoding="IBM437" standalone="yes"?>
<!--the first element follows.-->        
<root id="_1" d1p1:name="bar" xmlns:d1p1="foo">
  <element1>some characters</element1>
  <cdataElement date="10/3/2010 9:26:22 PM"><![CDATA[<this contains some 
characters XML wouldn't like & would choke on]]>&lt;this contains some 
characters XML wouldn't like &amp; so the XmlWriter replaces them</cdataElement>
  <emptyElement />
  <d1p1:emptyElement>
</d1p1:emptyElement>One string &amp; another.</root>
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Tags:

Readers Comments (0)




Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.


Latest Additions

Popular User Submitted Links

1


Hosted & Optimized JS Libraries – Cached Commons

1


Multi-File Uploader & Resizer – Agile Uploader

1


Open source online notebook-to-do-list manager

1


3 tier app design in silverlight using azure & RIA services

1


Polymaps – A JS Library For Image & Vector-Tiled Maps

eXTReMe Tracker