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:

  • 相关阅读:
    js文字跳动效果
    js文字效果
    centos7安装Logwatch配合msmtp邮件客户端发送服务器监控分析日志
    子查询
    Hexo添加字数统计、阅读时长
    基于visual Studio2013解决C语言竞赛题之0523魔方阵
    基于visual Studio2013解决C语言竞赛题之0522和为素
    基于visual Studio2013解决C语言竞赛题之0521圆盘求和
    基于visual Studio2013解决C语言竞赛题之0520相邻元素
    基于visual Studio2013解决C语言竞赛题之0519最大值
  • 原文地址:https://www.cnblogs.com/Jessy/p/2575928.html
Copyright © 2011-2022 走看看