zoukankan      html  css  js  c++  java
  • C# 写入XML方法

    我们要写入的XML文档内容为

    <?xml version="1.0" encoding="UTF-8"?> 
    <Contacts> 
      <Contact id="01"> 
        <Name>Daisy Abbey</Name> 
        <Gender>female</Gender> 
      </Contact>   
    </Contacts>

    (1)使用XmlDocument类:

    var xmlDoc = new XmlDocument(); 
    //Create the xml declaration first 
    xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
    
    //Create the root node and append into doc 
    var el = xmlDoc.CreateElement("Contacts"); 
    xmlDoc.AppendChild(el);
    
    // Contact 
    XmlElement elementContact = xmlDoc.CreateElement("Contact"); 
    XmlAttribute attrID = xmlDoc.CreateAttribute("id"); 
    attrID.Value = "01"; 
    elementContact.Attributes.Append(attrID);
    
    el.AppendChild(elementContact);
    
    // Contact Name 
    XmlElement elementName = xmlDoc.CreateElement("Name"); 
    elementName.InnerText = "Daisy Abbey"; 
    elementContact.AppendChild(elementName);
    
    // Contact Gender 
    XmlElement elementGender = xmlDoc.CreateElement("Gender"); 
    elementGender.InnerText = "female"; 
    elementContact.AppendChild(elementGender);
    
    xmlDoc.Save("test1.xml"); 

    (2)使用LINQ to XML 的XDocument类:

    var doc = new XDocument( 
        new XElement("Contacts", 
            new XElement("Contact", 
                new XAttribute("id", "01"),                
                new XElement("Name", "Daisy Abbey"), 
                new XElement("Gender", "female") 
            ) 
        ) 
    );    
    doc.Save("test2.xml"); 

    (3) 使用XmlTextWriter类:

    String filename = String.Concat("test3.xml");
    using (StreamWriter sw = new StreamWriter(filename))
    {
        // Create Xml Writer.
        XmlTextWriter xmlWriter = new XmlTextWriter(sw);
    
        // 也可以使用public XmlTextWriter(string filename, Encoding encoding)来构造
        // encoding默认为 UTF-8.
        //XmlTextWriter writer = new XmlTextWriter("test3.xml", null);
    
        // Set indenting so that its easier to read XML when open in Notepad and such apps. 
        xmlWriter.Formatting = Formatting.Indented;
    
        // This will output the XML declaration
        xmlWriter.WriteStartDocument();
                    
        xmlWriter.WriteStartElement("Contacts");
    
        xmlWriter.WriteStartElement("Contact");
        xmlWriter.WriteAttributeString("id", "01");
    
        xmlWriter.WriteElementString("Name", "Daisy Abbey");
        xmlWriter.WriteElementString("Gender", "female");                
    
        // close contact </contact>
        xmlWriter.WriteEndElement();
        // close contacts </contact>
        xmlWriter.WriteEndElement();
    
        xmlWriter.WriteEndDocument();
    
        xmlWriter.Close();
    }

    从上面的代码基本上还是可以看出来,使用LINQ to XML是最简便的。

  • 相关阅读:
    IDEA手动创建JFinal项目(404问题处理)
    php 把数字1-1亿换成汉字表述,例如 150 转成 一百五十
    模仿console自写函数打印js的对象
    每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。10元最多可喝多少瓶啤酒? php
    js-Event构造函数,也许你需要
    js将金额专成每隔3位数加逗号
    js-PC版监听键盘大小写事件
    用php脚本给html中引用的js和css路径打上版本
    通过js的console优雅的将php调试信息输出
    android中加载的html获取的宽高不正确
  • 原文地址:https://www.cnblogs.com/ShaYeBlog/p/2800896.html
Copyright © 2011-2022 走看看