zoukankan      html  css  js  c++  java
  • 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:

  • 相关阅读:
    Team Foundation Sidekicks 2010
    Asp.net页面传值的方式汇总
    轻量级IOC框架Ninject使用
    AutoMapper使用简单总结
    页面请求的方式(Get与Post)
    总结2012 规划2013
    在reset css后两个input之间还是出现默认间隔的问题。
    js学习笔记事件委托
    程序猿工具——svn
    JS 事件添加onclick写法注意。
  • 原文地址:https://www.cnblogs.com/Jessy/p/2575928.html
Copyright © 2011-2022 走看看