基于C#中XmlWriter写入Xml的深入分析
程序员文章站
2023-12-20 18:09:46
假定创建了xmlwriter的实例变量xmlwriter,下文中将使用此实例变量写xml1.如何使用xmlwriter写xml文档声明复制代码 代码如下:// writes...
假定创建了xmlwriter的实例变量xmlwriter,下文中将使用此实例变量写xml
1.如何使用xmlwriter写xml文档声明
// writestartdocument方法可以接受一个bool参数(表示standalone,是否为独立文档)或者不指定参数standalone保持默认值
xmlwriter.writestartdocument(false|true);
注意在使用writestartdocument方法后最好调用xmlwrite.writeenddocument()方法来关闭所有可能未关闭标签
2.如何使用xmlwriter写xml节点以及属性
//写节点
xmlwriter.writestartelement("cat");
//给节点添加属性
xmlwriter.writeattributestring("color", "white");
//给节点内部添加文本
xmlwriter.writestring("i'm a cat");
xmlwriter.writeendelement();
或者通过writeelementstring(string,string)方法写xml节点同时写下节点值,如下
//通过writeelementstring可以添加一个节点同时添加节点内容
xmlwriter.writeelementstring("pig", "pig is great");
3.如何写cdata
xmlwriter.writestartelement("dog");
//写cdata
xmlwriter.writecdata("<strong>dog is dog</strong>");
xmlwriter.writeendelement();
4.如何使用xmlwriter添加注释
xmlwriter.writecomment("this is an example writed by //www.jb51.net ");
5.如何设置xmlwriter的输出格式,解决输出utf-16问题
设置xml输出格式,需要通过xmlwritersettings类,如下代码
xmlwritersettings settings = new xmlwritersettings();
//要求缩进
settings.indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用encoding.utf8如果用encoding.utf8将在输出文本的最前面添加4个字节的非xml内容
settings.encoding = new utf8encoding(false);
//设置换行符
settings.newlinechars = environment.newline;
完整的代码示例如下:
using system;
using system.collections.generic;
using system.text;
using system.io;
using system.xml;
namespace usexmlwriter
{
class program
{
static void main(string[] args)
{
using (memorystream ms = new memorystream())
{
xmlwritersettings settings = new xmlwritersettings();
//要求缩进
settings.indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用encoding.utf8如果用encoding.utf8将在输出文本的最前面添加4个字节的非xml内容
settings.encoding = new utf8encoding(false);
//设置换行符
settings.newlinechars = environment.newline;
using (xmlwriter xmlwriter = xmlwriter.create(ms, settings))
{
//写xml文件开始<?xml version="1.0" encoding="utf-8" ?>
xmlwriter.writestartdocument(false);
//写根节点
xmlwriter.writestartelement("root");
//写字节点
xmlwriter.writestartelement("cat");
//给节点添加属性
xmlwriter.writeattributestring("color", "white");
//给节点内部添加文本
xmlwriter.writestring("i'm a cat");
xmlwriter.writeendelement();
//通过writeelementstring可以添加一个节点同时添加节点内容
xmlwriter.writeelementstring("pig", "pig is great");
xmlwriter.writestartelement("dog");
//写cdata
xmlwriter.writecdata("<strong>dog is dog</strong>");
xmlwriter.writeendelement();
xmlwriter.writecomment("this is an example writed by //www.jb51.net ");
xmlwriter.writeendelement();
xmlwriter.writeenddocument();
}
//将xml内容输出到控制台中
string xml = encoding.utf8.getstring(ms.toarray());
console.writeline(xml);
}
console.read();
}
}
}
1.如何使用xmlwriter写xml文档声明
复制代码 代码如下:
// writestartdocument方法可以接受一个bool参数(表示standalone,是否为独立文档)或者不指定参数standalone保持默认值
xmlwriter.writestartdocument(false|true);
注意在使用writestartdocument方法后最好调用xmlwrite.writeenddocument()方法来关闭所有可能未关闭标签
2.如何使用xmlwriter写xml节点以及属性
复制代码 代码如下:
//写节点
xmlwriter.writestartelement("cat");
//给节点添加属性
xmlwriter.writeattributestring("color", "white");
//给节点内部添加文本
xmlwriter.writestring("i'm a cat");
xmlwriter.writeendelement();
或者通过writeelementstring(string,string)方法写xml节点同时写下节点值,如下
复制代码 代码如下:
//通过writeelementstring可以添加一个节点同时添加节点内容
xmlwriter.writeelementstring("pig", "pig is great");
3.如何写cdata
复制代码 代码如下:
xmlwriter.writestartelement("dog");
//写cdata
xmlwriter.writecdata("<strong>dog is dog</strong>");
xmlwriter.writeendelement();
4.如何使用xmlwriter添加注释
复制代码 代码如下:
xmlwriter.writecomment("this is an example writed by //www.jb51.net ");
5.如何设置xmlwriter的输出格式,解决输出utf-16问题
设置xml输出格式,需要通过xmlwritersettings类,如下代码
复制代码 代码如下:
xmlwritersettings settings = new xmlwritersettings();
//要求缩进
settings.indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用encoding.utf8如果用encoding.utf8将在输出文本的最前面添加4个字节的非xml内容
settings.encoding = new utf8encoding(false);
//设置换行符
settings.newlinechars = environment.newline;
完整的代码示例如下:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.text;
using system.io;
using system.xml;
namespace usexmlwriter
{
class program
{
static void main(string[] args)
{
using (memorystream ms = new memorystream())
{
xmlwritersettings settings = new xmlwritersettings();
//要求缩进
settings.indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用encoding.utf8如果用encoding.utf8将在输出文本的最前面添加4个字节的非xml内容
settings.encoding = new utf8encoding(false);
//设置换行符
settings.newlinechars = environment.newline;
using (xmlwriter xmlwriter = xmlwriter.create(ms, settings))
{
//写xml文件开始<?xml version="1.0" encoding="utf-8" ?>
xmlwriter.writestartdocument(false);
//写根节点
xmlwriter.writestartelement("root");
//写字节点
xmlwriter.writestartelement("cat");
//给节点添加属性
xmlwriter.writeattributestring("color", "white");
//给节点内部添加文本
xmlwriter.writestring("i'm a cat");
xmlwriter.writeendelement();
//通过writeelementstring可以添加一个节点同时添加节点内容
xmlwriter.writeelementstring("pig", "pig is great");
xmlwriter.writestartelement("dog");
//写cdata
xmlwriter.writecdata("<strong>dog is dog</strong>");
xmlwriter.writeendelement();
xmlwriter.writecomment("this is an example writed by //www.jb51.net ");
xmlwriter.writeendelement();
xmlwriter.writeenddocument();
}
//将xml内容输出到控制台中
string xml = encoding.utf8.getstring(ms.toarray());
console.writeline(xml);
}
console.read();
}
}
}