XML 读写(命名空间)
2012-07-04 11:38
375 查看
http://www.w3school.com.cn/xml/xml_namespaces.asp (XML命名空间和xml详细内容)
http://www.w3.org/TR/REC-xml-names/
处理含有xml命名空间的读取
/article/5382409.html
http://msdn.microsoft.com/zh-cn/library/system.xml.xmlwriter.aspx (.net实现xml读写)
UTF8:
Many Windows programs (including Windows Notepad) add the bytes 0xEF, 0xBB, 0xBF at the start of any document saved as UTF-8. This is the UTF-8 encoding of the Unicode byte order mark (BOM), and is commonly referred to as a UTF-8 BOM, even though it is not relevant to byte order. The BOM can also appear if another encoding with a BOM is translated to UTF-8 without stripping it. Older text editors may display the BOM as "" at the start of the document.
static void Main(string[] args)
{
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
//要求缩进
settings.Indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用Encoding.UTF8如果用Encoding.UTF8将在输出文本的最前面添加3个字节的非xml内容
settings.Encoding = Encoding.UTF8;
//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 bob");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
//将xml内容输出到控制台中
string xml = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(xml);
}
Console.Read();
}
结果 1. 使用UTF8:
http://www.w3.org/TR/REC-xml-names/
处理含有xml命名空间的读取
/article/5382409.html
http://msdn.microsoft.com/zh-cn/library/system.xml.xmlwriter.aspx (.net实现xml读写)
UTF8:
Many Windows programs (including Windows Notepad) add the bytes 0xEF, 0xBB, 0xBF at the start of any document saved as UTF-8. This is the UTF-8 encoding of the Unicode byte order mark (BOM), and is commonly referred to as a UTF-8 BOM, even though it is not relevant to byte order. The BOM can also appear if another encoding with a BOM is translated to UTF-8 without stripping it. Older text editors may display the BOM as "" at the start of the document.
static void Main(string[] args)
{
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
//要求缩进
settings.Indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用Encoding.UTF8如果用Encoding.UTF8将在输出文本的最前面添加3个字节的非xml内容
settings.Encoding = Encoding.UTF8;
//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 bob");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
//将xml内容输出到控制台中
string xml = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(xml);
}
Console.Read();
}
结果 1. 使用UTF8:
相关文章推荐
- 34. .net中读写XML的类都归属于哪些命名空间?
- xml(带有命名空间的)读写操作
- xml(带有命名空间的)读写操作
- 在jquery中处理带有命名空间的XML数据
- XML指南——XML 命名空间
- XML和Schema命名空间详解
- [转] Spring4.3.x 浅析xml配置的解析过程(6)——解析context命名空间之property-placeholder和property-override标签
- 带有命名空间的xml 操作
- xml的schema命名空间之研究
- DOM4J处理XML带有命名空间的四种方法
- Spring--IoC---基本XML的DI---P命名空间设值注入
- XML——SelectSingleNode 获取有命名空间的节点的值
- 读写XML文档时,去掉新增加节点的“空命名空间”(xmlns=””)
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- 去掉xml中的命名空间
- XML 命名空间(XML Namespaces)
- XML之命名空间[转载]
- xml命名空间
- Spring4.3.x 浅析xml配置的解析过程(6)——解析context命名空间之property-placeholder和property-override标签
- XML 命名空间(XML Namespaces)介绍以及节点读取方法