C#操作Xml:使用XmlWriter写Xml
假定创建了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");//写CDataxmlWriter.WriteCData("<strong>dog is dog</strong>");xmlWriter.WriteEndElement(); |
4.如何使用XmlWriter添加注释
|
|
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; |
完整的代码示例如下:
/*玉开技术博客 http://www.cnblogs.com/yukaizhao */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.WriteEndElement(); xmlWriter.WriteEndDocument(); } //将xml内容输出到控制台中 string xml = Encoding.UTF8.GetString(ms.ToArray()); Console.WriteLine(xml); } Console.Read(); } }} |
C#处理Xml的相关随笔:
C#操作Xml:使用XmlWriter写Xml的更多相关文章
- 使用XmlWriter写Xml
假定创建了XmlWriter的实例变量xmlWriter,下文中将使用此实例变量写Xml 1.如何使用XmlWriter写Xml文档声明 ? // WriteStartDocument方法可以接受一个 ...
- c# 用XmlWriter写xml序列化
using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using ...
- C#操作Xml:linq to xml操作XML
LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...
- C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)【转载】
已知有一个XML文件(bookstore.xml)如下: Corets, Eva 5.95 1.插入节点 往节点中插入一个节点: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 使用XmlWriter创建XML文件
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml ...
- 使用dom4j写xml文件——源码
1 dom4j下载与配置 1.1 dom4j下载 请移步下载链接 1.2 maven依赖 <dependency> <groupId>org.dom4j</groupId ...
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
Oracle 远程访问配置 服务端配置 如果不想自己写,可以通过 Net Manager 来配置. 以下配置文件中的 localhost 改为 ip 地址,否则,远程不能访问. 1.网络监听配置 ...
- 【C++】【TinyXml】xml文件的读写功能使用——写xml文件
TinyXml工具是常用比较简单的C++中xml读写的工具 需要加载 #include "TinyXml\tinyxml.h" 在TinyXML中,根据XML的各种元素来定义了一些 ...
- 2018-8-10-win10-uwp-读写XML
title author date CreateTime categories win10 uwp 读写XML lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...
随机推荐
- lua-TestMore(转)
http://fperrad.github.io/lua-TestMore/ http://www.softpedia.com/get/Programming/Debuggers-Decompiler ...
- (转)Maven最佳实践:划分模块
“分天下为三十六郡,郡置守,尉,监” —— <史记·秦始皇本纪> 所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块 ...
- Android 深入解析光传感器(二)
光线传感器演示1 讲了一大堆的理论,那么以下的样例就来展示一下光线感应器的使用.为什么充分展现光感的用法,我这个样例写的很easy,仅仅写了使用光感必须的代码,然后用了几个textVie ...
- 这两个成员函数inline重新virtual种类
inlineType表示在编译时扩展功能,随着在函数调用的函数体而出替换函数调用:和vitual它是c++多态的必要条件,但为了表现出多态性,您将需要等到执行,要知道什么是真正的函数调用.从表面上看这 ...
- C#的WebBrowser控制浏览
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- jwt (JSON Web Token)官方说明
http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32#appendix-A 相关文章 http://haomou.net/2014 ...
- Windows Auzre 微软的云计算产品的后台操作界面
Windows Auzre 微软的云计算产品的后台操作界面,试用期,相比于阿里云后台操作不是人. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTmFvbG ...
- asm 盘头损失,破坏
BUG 14693394 – ORA-15196: INVALID ASM BLOCK HEADER [KFC.C:26076] [ENDIAN_KFBH] BUG 14758001 – ORA-15 ...
- War文件部署(转)
其实,开始要求将源码压缩成War文件时,一头雾水! 公司项目要求做CAS SSO单点登录 也就是这玩意.... 其实war文件就是Java中web应用程序的打包.借用一个老兄的话,“当你一个web应用 ...
- 【三】注入框架RoboGuice使用:(Your First Resource Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...