zoukankan      html  css  js  c++  java
  • XML文件操作类--创建XML文件

          这个类是在微软XML操作类库上进行的封装,只是为了更加简单使用,包括XML类创建节点的示例。

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    
    namespace testForm
    {
        class Operation_APPCFG
        {
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlelem;
            XmlDeclaration xmldecl;
    
            /// <summary>
            /// 构造函数
            /// </summary>
            public Operation_APPCFG()
            {
                xmldoc = new XmlDocument();
            }
            /// <summary>
            /// 创建XML文件的段落声明
            /// </summary>
            /// <param name="strEncoding">XML编码方式,输入"gb2312"、"utf-8"</param>
            /// <param name="strStandalone">独立特性,输入"yes"、"no"或"null",默认为"null"</param>
            public void CreateDeclaration(string strEncoding,string strStandalone)
            {
                if ((strEncoding == null) || (strEncoding == ""))
                {
                    strEncoding = null;
                }
                if ((strStandalone == null) || (strStandalone == ""))
                {
                    strStandalone = null;
                }
                else
                {
                    if (!strStandalone.Equals("yes") || !strStandalone.Equals("no"))
                    {
                        strStandalone = null;
                    }
                }
                xmldecl = xmldoc.CreateXmlDeclaration("1.0", strEncoding, strStandalone);
                xmldoc.AppendChild(xmldecl);
            }
    
            /// <summary>
            /// 创建root元素
            /// </summary>
            /// <param name="localName">root元素的名称</param>
            public void CreateElement(string localName)
            {
                xmlelem = xmldoc.CreateElement("", localName, "");
                xmldoc.AppendChild(xmlelem);
            }
    
            /// <summary>
            /// 创建节点的子节点,模式 <Node subA="subA" subB="subB"></Node>
            /// </summary>
            /// <param name="parentName">父节点的名称</param>
            /// <param name="NodeName">节点的名称</param>
            /// <param name="hash">需创建的子节点的hashtable</param>
            public void CreateNodeModeA(string parentName, string NodeName, Hashtable hash)
            {
                XmlNode root = xmldoc.SelectSingleNode(parentName);
                XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点 
                foreach (DictionaryEntry de in hash)
                {
                    xe1.SetAttribute(de.Key.ToString(), de.Value.ToString());//设置该节点genre属性 //xe1.SetAttribute("ISBN", "2-3631-4");
                }
                root.AppendChild(xe1);
            }
            /// <summary>
            /// 创建节点的子节点(泛型方法),模式 <Node subA="subA" subB="subB"></Node>
            /// </summary>
            /// <param name="parentName">父节点的名称</param>
            /// <param name="NodeName">节点的名称</param>
            /// <param name="hash">需创建的子节点的hashtable</param>
            public void CreateNodeModeB(string parentName,string NodeName, Dictionary<string, string> hash )
            {
                XmlNode root = xmldoc.SelectSingleNode(parentName);
                XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点 
                foreach (KeyValuePair<string, string> de in hash)
                {
                    xe1.SetAttribute(de.Key.ToString(), de.Value.ToString());//设置该节点genre属性 //xe1.SetAttribute("ISBN", "2-3631-4");
                }
                root.AppendChild(xe1);
            }
    
            /// <summary>
            /// 创建节点的子节点(泛型方法),模式 <title>入门到精通</title>
            /// </summary>
            /// <param name="NodeName">节点的名称</param>
            /// <param name="hash">需创建的子节点的hashtable</param>
            public void CreateNodeModeC(string parentName,Hashtable hash)
            {
                XmlNode root = xmldoc.SelectSingleNode(parentName);
                XmlElement xesub1;
                foreach (DictionaryEntry de in hash)
                {
                    xesub1 = xmldoc.CreateElement(de.Key.ToString());
                    xesub1.InnerText = de.Value.ToString();//设置文本节点 
                    root.AppendChild(xesub1);
                }
            }
            /// <summary>
            /// 保存文件
            /// </summary>
            /// <param name="pathFile"></param>
            public void SaveFile(string pathFile)
            {
                xmldoc.Save(pathFile);
            }
    
        }
    }

       下面是网上一位网友写的示例,参考意义相同,原文地址http://www.cnblogs.com/txw1958/archive/2013/01/16/csharp-xml.html

            public void CreateFile()
            {
                
                //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
                XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", null, null);//encoding mode:"gb2312"、"utf-8",也可以为null
                xmldoc.AppendChild(xmldecl);
    
                //加入一个根元素
                xmlelem = xmldoc.CreateElement("", "configuration", "");
                xmldoc.AppendChild(xmlelem);
                //加入另外一个元素
                for (int i = 1; i < 3; i++)
                {
    
                    XmlNode root = xmldoc.SelectSingleNode("Employees");//查找<Employees> 
                    XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点 
                    xe1.SetAttribute("genre", "DouCube");//设置该节点genre属性 
                    xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性 
    
                    XmlElement xesub1 = xmldoc.CreateElement("title");
                    xesub1.InnerText = "CS从入门到精通";//设置文本节点 
                    xe1.AppendChild(xesub1);//添加到<Node>节点中 
                    XmlElement xesub2 = xmldoc.CreateElement("author");
                    xesub2.InnerText = "候捷";
                    xe1.AppendChild(xesub2);
                    XmlElement xesub3 = xmldoc.CreateElement("price");
                    xesub3.InnerText = "58.3";
                    xe1.AppendChild(xesub3);
    
                    root.AppendChild(xe1);//添加到<Employees>节点中 
                }
                //保存创建好的XML文档
                xmldoc.Save("DB.cfg");
            }

           APP.config 本质上也是一个XML文件,下面是一些参考资料

           http://www.cnblogs.com/zfanlong1314/p/3623622.html

           https://msdn.microsoft.com/zh-cn/library/system.configuration.configuration(v=vs.80).aspx

           http://www.cnblogs.com/bynet/archive/2010/06/10/1755721.html

          其它参考资料

      http://blog.csdn.net/albertliangyg/article/details/8633521

      https://msdn.microsoft.com/zh-cn/library/system.text.encoding(v=vs.80).aspx

      https://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument.createelement(v=vs.100).aspx

      https://msdn.microsoft.com/zh-cn/library/5tbh8a42

  • 相关阅读:
    PAT B1027 打印沙漏 (20 分)
    PAT B1025 反转链表 (25 分)
    PAT B1022 D进制的A+B (20 分)
    PAT B1018 锤子剪刀布 (20 分)
    PAT B1017 A除以B (20 分)
    PAT B1015 德才论 (25 分)
    PAT B1013 数素数 (20 分)
    PAT B1010 一元多项式求导 (25 分)
    HDU 1405 The Last Practice
    HDU 1165 Eddy's research II
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/4643612.html
Copyright © 2011-2022 走看看