Writing XML File using XmlWriter
来源:互联网 发布:淘宝王府井百货靠谱么 编辑:程序博客网 时间:2024/05/18 05:51
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
- Writing XML File using XmlWriter
- Reading and Writing using a Large Random Access File
- 用 XmlWriter 编写 XML
- 使用XmlWriter写Xml
- xml学习之 XmlWriter
- 使用XmlWriter写Xml
- 使用XmlWriter写Xml
- Viewing and Writing XML Data using ADO.NET DataSets
- c# XmlWriter XmlTextWriter操作XML
- XmlWriter
- XMLWriter
- XMLWriter
- Writing file-写入文件
- 在Csharp中用XmlWriter写XML文件
- WP7 IsolatedStorage--通过XmlWriter读写XML文件
- C#中使用XmlWriter写入XML数据
- C#使用XmlWriter写入XML文件
- XmlWriter输出xml文档编码格式问题
- 将汉字数字转换成阿拉伯数字
- [转贴] 对UTF8编码的初步认识
- 任正非:不要做一个完人 做完人很痛苦的
- Swing笔记 2. SwingUtilities , invokeLater , 线程安全
- 去闯吧,反正我一无所有
- Writing XML File using XmlWriter
- 日本地震为什么没有一间教室倒塌
- Java技术开源搜索引擎
- 比你手写代码更快的AJAX开发
- 阻止网页挂马的若干工具 - 信息安全专栏 -- 褚诚云 - CSDNBlog
- 开发者版本:你属于哪个版本的程序员?
- Deployment Server / Active Directory Synchronization
- JAVA程序员之路
- [客运中国]上线了