Writing XML File using XmlWriter
2008-06-26 09:00
246 查看
The XmlWriter and XmlTextWriter classes are defined in the System.XML namespace.
The XmlTextWriter class is derived from XmlWriter class, which represents a writer that provides fast non-cached forward-only way of generating XML documents based on the W3C Extensible Markup Language (XML) 1.0 specification.
In this article, I will show you how to use XmlTextWriter class to create an XML document and write data to the document.
Adding namespace Reference
Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project.
using
The constructor of the XmlTextWriter class creates an XML file if file doesn't exist. In this sample, I create a new XML file called xmltest.xml in C/temp directory.
XmlTextWriter writer =
XmlTextWriter writer = new XmlTextWriter(Console.Out);
The WriteStartDocument method starts a new document. The WriteStartElement and the WriteEndElement pair is used to add a new element to the document. The WriteString writes a string to the document.
writer.WriteStartDocument(); writer.WriteComment("Commentss: XmlWriter Test Program"); writer.WriteProcessingInstruction("Instruction","Person Record"); writer.WriteStartElement("p", "person", "urn:person"); writer.WriteStartElement("LastName",""); writer.WriteString("Chand"); writer.WriteEndElement(); writer.WriteElementInt16("age","", 25); writer.WriteEndDocument();
namespace
{ using System;
using System.Xml;
/// <summary> /// Summary description for Class1. /// </summary>
public class Class1 {
public Class1() { }
public static int Main(string[] args)
{
try
{
// Creates an XML file is not exist XmlTextWriter writer = new XmlTextWriter("C://temp//xmltest.xml", null);
// Starts a new document writer.WriteStartDocument(); //Write comments writer.WriteComment("Commentss: XmlWriter Test Program"); writer.WriteProcessingInstruction("Instruction","Person Record");
// Add elements to the file
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand"); writer.WriteEndElement();
writer.WriteStartElement("FirstName","");
writer.WriteString("Mahesh");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
// Ends the document
writer.WriteEndDocument();
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
return 0;
}
}
}
Attachment createxml1.cs 2 KB
WriteToXML Souce Code: Adding Data to the Documentnew XmlTextWriter("C://temp//xmltest.xml", null);NOTE: If you don't want to write data in an XML file and want to display XML contents on the Console, pass Console.Out as a parameter of the constructor. System.Xml;Creating an XML Document
The XmlTextWriter class is derived from XmlWriter class, which represents a writer that provides fast non-cached forward-only way of generating XML documents based on the W3C Extensible Markup Language (XML) 1.0 specification.
In this article, I will show you how to use XmlTextWriter class to create an XML document and write data to the document.
Adding namespace Reference
Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project.
using
The constructor of the XmlTextWriter class creates an XML file if file doesn't exist. In this sample, I create a new XML file called xmltest.xml in C/temp directory.
XmlTextWriter writer =
XmlTextWriter writer = new XmlTextWriter(Console.Out);
The WriteStartDocument method starts a new document. The WriteStartElement and the WriteEndElement pair is used to add a new element to the document. The WriteString writes a string to the document.
writer.WriteStartDocument(); writer.WriteComment("Commentss: XmlWriter Test Program"); writer.WriteProcessingInstruction("Instruction","Person Record"); writer.WriteStartElement("p", "person", "urn:person"); writer.WriteStartElement("LastName",""); writer.WriteString("Chand"); writer.WriteEndElement(); writer.WriteElementInt16("age","", 25); writer.WriteEndDocument();
namespace
{ using System;
using System.Xml;
/// <summary> /// Summary description for Class1. /// </summary>
public class Class1 {
public Class1() { }
public static int Main(string[] args)
{
try
{
// Creates an XML file is not exist XmlTextWriter writer = new XmlTextWriter("C://temp//xmltest.xml", null);
// Starts a new document writer.WriteStartDocument(); //Write comments writer.WriteComment("Commentss: XmlWriter Test Program"); writer.WriteProcessingInstruction("Instruction","Person Record");
// Add elements to the file
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("LastName","");
writer.WriteString("Chand"); writer.WriteEndElement();
writer.WriteStartElement("FirstName","");
writer.WriteString("Mahesh");
writer.WriteEndElement();
writer.WriteElementInt16("age","", 25);
// Ends the document
writer.WriteEndDocument();
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
return 0;
}
}
}
Attachment createxml1.cs 2 KB
WriteToXML Souce Code: Adding Data to the Documentnew XmlTextWriter("C://temp//xmltest.xml", null);NOTE: If you don't want to write data in an XML file and want to display XML contents on the Console, pass Console.Out as a parameter of the constructor. System.Xml;Creating an XML Document
相关文章推荐
- Writing XML File using XmlWriter
- Csharp: read excel file using Open XML SDK 2.5
- Generate the java class from XML Schema file using JAXB
- Using Oracle XML DB Repository as a Filesystem
- OpenCV_Tutorials——CORE MODULE.THE CORE FUNCTIONALITY—— File Input and Output using XML and YAML files
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- copy file using FileReader/Writer.
- Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
- Csharp: read excel file using Open XML SDK 2.5
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268
- Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable
- [C#] Format a XML string using XmlTextWriter
- Convert an ABAP table into XML file using SAP DOM Approach
- Write to file using a BufferedWriter
- Reading and Writing using a Large Random Access File
- Generate the java class from XML Schema file using JAXB
- Csharp: create word file using Open XML SDK 2.5
- [iOS开发站在巨人肩膀上]之iPhone Images from URL using XML File
- Convert an XML file into an ABAP table using SAP DOM Approach
- Create XML using class XmlTextWriter