【C# 创建xml 】新手篇:通过XmlDocument读写Xml文档

Xml是扩展标记语言的简写,是一种开发的文本格式。

下面我将介绍三种常用的读取XML文件的方法。分别是 
   1: 使用 XmlDocument
   2: 使用 XmlTextReader

下面我们使用XmlDocument:
 

<?xml version="1.0" encoding="UTF-8"?>
<skills>
  <id>2</id>
  <name>李东阳</name>
</skills>
        //创建一个XML文档
        public void CreateXml()
        {
            //生成文档
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));//xml文件头
            //创建根元素
            XmlElement root = xmlDoc.CreateElement("skills");
            //添加到父元素上
            xmlDoc.AppendChild(root);
            //穿件ID元素
            XmlElement id = xmlDoc.CreateElement("id");
            //设置内容
            id.InnerText = 2.ToString();
            //添加到节点
            root.AppendChild(id);
            XmlElement name = xmlDoc.CreateElement("name");
            name.InnerText = "李东阳";
            root.AppendChild(name);
            //保存内容
            xmlDoc.Save(@"G:\2019年项目开发\01【2019年项目开发】-新版\测试专用\XML专用\" + "MyInfo.xml");
            MessageBox.Show("保存成功!");//提示用户保存成功

                                     //刷新
                                     // AssetDatabase.Refresh();
        }
<?xml version="1.0" encoding="UTF-8"?>
<Config>
  <Path>E:\Test\</Path>
  <Regex>&lt;![CDDATA[@^abc$]]&gt;</Regex>
  <ini timeout="200">time</ini>
</Config>
//创建一个XML文档
        public void WriteXml()
        {

            //首先创建 XmlDocument xml文档 
            XmlDocument xml = new XmlDocument();

            xml.AppendChild(xml.CreateXmlDeclaration("1.0", "UTF-8", null));//xml文件头

            //创建根节点 config           
            XmlElement config = xml.CreateElement("Config");
            //把根节点加到xml文档中            
            xml.AppendChild(config);
            //创建一个节点 path(用于做子节点)           
            XmlElement path = xml.CreateElement("Path");
            //path节点中的文本内容为 E:\Test\ @用于转义后面的'\'            
            path.InnerText = @"E:\Test\";
            //将path添加为config的子节点            
            config.AppendChild(path);
            //以下Regex同理            
            XmlElement regex = xml.CreateElement("Regex");
            regex.InnerText = "<![CDDATA[@^abc$]]>";
            config.AppendChild(regex);
            XmlElement ini = xml.CreateElement("ini");
            //所以我们需要创建 ini标签里的xml属性 属性名为timeout            
            XmlAttribute timeout = xml.CreateAttribute("timeout");
            //timeout属性的内容为200            
            timeout.InnerText = "200";
            //标签ini里的文档内容为 time            
            ini.InnerText = "time";
            //创建完标签的属性timeout 后需要将其添加到ini标签的属性里            
            ini.Attributes.Append(timeout);
            //最后将ini标签添加到config 父节点里           
            config.AppendChild(ini);
            //最后将整个xml文件保存在D盘             
            xml.Save(@"G:\2019年项目开发\01【2019年项目开发】-新版\测试专用\XML专用\abc.xml");
            MessageBox.Show("保存成功!");//提示用户保存成功
        }
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<students>
  <student name="张同学">
    <courses>
      <course name="语文">
        <teacherComment><![CDATA[<font color ="red">这是语文老师的批注</font>]]></teacherComment>
      </course>
    </courses>
  </student>
</students>
 public void WriteXmls()
        {
            XmlDocument xmlDoc = new XmlDocument();
            //创建Xml声明部分,即<?xmlversion = "1.0" encoding = "utf-8" ?>
            XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            XmlElement root = xmlDoc.DocumentElement;
            xmlDoc.InsertBefore(xmldecl, root);

            //创建根节点
            XmlNode rootNode = xmlDoc.CreateElement("students");




            //创建student子节点
            XmlNode studentNode = xmlDoc.CreateElement("student");
            //创建一个属性
            XmlAttribute nameAttribute = xmlDoc.CreateAttribute("name");
            nameAttribute.Value = "张同学";
            //xml节点附件属性
            studentNode.Attributes.Append(nameAttribute);


            //创建courses子节点
            XmlNode coursesNode = xmlDoc.CreateElement("courses");
            XmlNode courseNode1 = xmlDoc.CreateElement("course");
            XmlAttribute courseNameAttr = xmlDoc.CreateAttribute("name");
            courseNameAttr.Value = "语文";
            courseNode1.Attributes.Append(courseNameAttr);
            XmlNode teacherCommentNode = xmlDoc.CreateElement("teacherComment");
            //创建Cdata块
            XmlCDataSection cdata = xmlDoc.CreateCDataSection("<font color =\"red\">这是语文老师的批注</font>");
            teacherCommentNode.AppendChild(cdata);
            courseNode1.AppendChild(teacherCommentNode);
            coursesNode.AppendChild(courseNode1);
            //附加子节点
            studentNode.AppendChild(coursesNode);

            rootNode.AppendChild(studentNode);
            //附加根节点
            xmlDoc.AppendChild(rootNode);

            //保存Xml文档
            xmlDoc.Save(@"G:\2019年项目开发\01【2019年项目开发】-新版\测试专用\XML专用\test.xml");

            MessageBox.Show("已保存Xml文档");

        }

猜你喜欢

转载自blog.csdn.net/zgscwxd/article/details/86640700