您的位置:首页 > 其它

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:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: