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:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值