zoukankan      html  css  js  c++  java
  • C# -- 使用XmlDocument或XDocument创建xml文件

    使用XmlDocument或XDocument创建xml文件

    需引用:System.Xml; System.Xml.Linq;

    1.使用XmlDocument创建xml(入门案例)

     1         static void Main(string[] args)
     2         {
     3             //使用XmlDocument创建xml
     4             XmlDocument xmldoc = new XmlDocument();
     5             XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
     6             xmldoc.AppendChild(xmldec);
     7 
     8             //添加根节点
     9             XmlElement rootElement = xmldoc.CreateElement("school");
    10             xmldoc.AppendChild(rootElement);
    11 
    12             //添加根节点下的子节点元素
    13             XmlElement classElement = xmldoc.CreateElement("class");
    14             rootElement.AppendChild(classElement);
    15             XmlAttribute atrrClass = xmldoc.CreateAttribute("No");
    16             atrrClass.Value = "1";
    17             classElement.Attributes.Append(atrrClass);
    18 
    19             //添加子节点下的元素
    20             XmlElement stuElement = xmldoc.CreateElement("student");
    21             classElement.AppendChild(stuElement);
    22             XmlAttribute attrStu = xmldoc.CreateAttribute("sid");
    23             attrStu.Value = "20180101";
    24             stuElement.Attributes.Append(attrStu);
    25 
    26             //保存文件
    27             xmldoc.Save(@"d:zzzTestA.xml");
    28             Console.WriteLine("创建xml文件ok!");
    29             Console.ReadKey();
    30 
    31         }


    使用XmlDocument创建的xml文件:

    2. 使用XDocument创建xml(入门案例)

     1         static void Main(string[] args)
     2         {
     3             //使用XDocument创建xml
     4             System.Xml.Linq.XDocument xdoc = new XDocument();
     5             XDeclaration xdec = new XDeclaration("1.0", "utf-8", "yes");
     6             xdoc.Declaration = xdec;
     7 
     8             //添加根节点
     9             XElement rootEle = new XElement("school");
    10             xdoc.Add(rootEle);
    11 
    12             //给根节点添加子节点
    13             XElement classEle = new XElement("class");
    14             XAttribute attrClass = new XAttribute("No", 1);
    15             classEle.Add(attrClass);
    16             rootEle.Add(classEle);
    17 
    18             //添加子节点下的元素
    19             XElement stuEle = new XElement("student");
    20             XAttribute atrStu = new XAttribute("sid", "20180101");
    21             stuEle.Add(atrStu);
    22             classEle.Add(stuEle);
    23 
    24             //保存文件
    25             xdoc.Save("d:\zzz\TestB.xml");
    26             Console.WriteLine("创建xml文件ok");
    27             Console.ReadKey();
    28         }

    使用XDocument创建的Xml文件:

  • 相关阅读:
    git基础使用小记
    MYSQL 安装&配置
    NGINX 安装&配置
    PHP编译安装
    linux基本命令操作
    css清除浮动的8种方法以及优缺点
    简单概括下浏览器事件模型,如何获得资源dom节点
    HTML5新增的表单元素有哪些?
    css 引入的方式有哪些, link和@import的区别是什么
    git与svn的区别
  • 原文地址:https://www.cnblogs.com/ChengWenHao/p/CreateXml.html
Copyright © 2011-2022 走看看