XML 读写(命名空间)
?
http://www.w3school.com.cn/xml/xml_namespaces.asp (XML命名空間和xml詳細內容)
?
http://www.w3.org/TR/REC-xml-names/?
處理含有xml命名空間的讀取
http://www.cnblogs.com/martin-chen/archive/2011/02/24/xml-studynote-namespace.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:
?
轉載于:https://www.cnblogs.com/Jessy/archive/2012/07/04/2575928.html
總結
以上是生活随笔為你收集整理的XML 读写(命名空间)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1.简单工厂模式
- 下一篇: 云从科技从容大模型发布 可提供参考资料来