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 ...
随机推荐
- OCP解决问题052-- DROP PROFILE app_user
133.You created a profile APP_USER and assigned it to the users. After a month, you decide to drop t ...
- 快速排序:升序+降序----java实现
快速排序思路:先把第一个元素令为low下标,最后一个为high下标.并把第一个元素令为temp来作为标准元素.以标准元素来调整数组,使比标准元素小的都在标准元素前,比标准元素大的都在标准元素后.这样一 ...
- mongodb 学习笔记05 --用户管理
csdn的markdown编辑器真有够烂的,这篇文章又给弄丢了 启用认证 mongod 启动默认没有开启权限,你须要指定 –auth 启动.或者在配置文件里设置security.authorizati ...
- Android 浏览器开发WebView setBlockNetworkImage本末
含义本身防止网络数据图片 webSettings.setBlockNetworkImage(true); 停止发布数据 webSettings.setBlockNetworkImage(false); ...
- c#中 uint--byte[]--char[]--string相互转换汇总
原文:c#中 uint--byte[]--char[]--string相互转换汇总 在在做一些互操作的时候往往需要一些类型的相互转换,比如用c#访问win32api的时候往往需要向api中传入DWOR ...
- 【LeetCode】【Python解读】Container with most water
这个问题是芭芭拉在采访中遇到的,不幸的是,的复杂性O(n2)该,太失望了,难怪没有通过面试. Given n non-negative integers a1, a2, ..., an, where ...
- Oracle 初始化参数文件pfile和spfile
pfile和spfile差额 pfile :Oracle 9i之前.ORACLE使用我们一直PFILE存储的初始化参数,,能够在操作系统级别改动. 当spfile文件改动出现错误导致oracle无法启 ...
- java提高篇(十三)-----字符串
可以证明,字符串操作是计算机程序设计中最常见的行为. 一.String 首先我们要明确,String并不是基本数据类型,而是一个对象,并且是不可变的对象.查看源码就会发现String类为f ...
- html标和下标应用
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 深入了解C++中间mutablekeyword
深入理解C++中的mutablekeyword kezunhai@gmail.com http://blog.csdn.net/kezunhai keywordmutable是C++中一个不 ...