zoukankan      html  css  js  c++  java
  • XML文件的操作

            /// <summary>
            /// 创建一个xml文件/// </summary>
            /// 根节点<local svduver="2.0">
            /// <param name="filepath">文件路径</param>
            public static void CreateFileSaveLocal(string filepath)
            {
                XmlDocument xmlDoc = new XmlDocument();
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  local节点带属性值svduver="2.0"
                XmlNode root = xmlDoc.CreateElement("local");
                xmlDoc.AppendChild(root);
                // 添加节点属性
                XmlNode attrCount = xmlDoc.CreateNode(XmlNodeType.Attribute, "svduver", null);
                attrCount.Value = "2.0";
                root.Attributes.SetNamedItem(attrCount);
    XmlNode attrMeg
    = xmlDoc.CreateNode(XmlNodeType.Attribute, "describe", null); attrMeg.Value = "带属性值的节点"; root.Attributes.SetNamedItem(attrMeg); //XmlElement root = xmlDoc.CreateElement(Node_Local); //root.SetAttribute("svduver", "2.0"); //xmlDoc.AppendChild(root); //保存创建的XML文件 xmlDoc.Save(filepath); }

    写入xml文件

    上图代码创建的xml文件格式如下图:上图只创建了根节点

            /// <summary>
            /// 将设置的变量数据写入xml文件中/// </summary>
            /// <param name="filepath">文件路径</param>
            /// <param name="varName">变量名称</param>
            /// <param name="varType">变量类型</param>
            /// <param name="varINI">上电初始值</param>
            /// <param name="varYes">有效值</param>
            /// <param name="varMeg">描述</param>
            public static void WriteLocalVariableToXml(string filepath, List<string> varName, List<string> varType, List<string> varINI,
                List<string> varYes, List<string> varMeg)
            {
                if (File.Exists(filepath))
                {
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(filepath);
                    XmlNode rootNode = xmldoc.SelectSingleNode("local");//XML文档根节点
                    //rootNode.RemoveAll();
                    for (int i = 0; i < varName.Count; i++)
                    {
                        XmlElement element1 = xmldoc.CreateElement("Variable");
                        element1.SetAttribute("Name", varName[i]);
                        element1.SetAttribute("Type", varType[i]);
                        element1.SetAttribute("Val", varYes[i]);
                        element1.SetAttribute("Ini", varINI[i]);
                        element1.SetAttribute("Meg", varMeg[i]);
                        rootNode.AppendChild(element1);
                    }
                    xmldoc.Save(filepath);
                }
            }

     如果是标准的XML (如下图),读取就方便的多:

    创建标准的xml:

    public static void CreateDefaultLocal(string filepath)
            {
                 XmlDocument xmlDoc = new XmlDocument();
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点
                XmlNode root = xmlDoc.CreateElement("Defalut");
                xmlDoc.AppendChild(root);
                //根节点下创建TextBox节点
                XmlNode tb = xmlDoc.CreateElement("TextBox");
                root.AppendChild(tb);
                // 添加节点属性
                XmlNode attrCount2 = xmlDoc.CreateNode(XmlNodeType.Attribute, "Name", null);
                attrCount2.InnerText = "文本框默认值设置";
                tb.Attributes.SetNamedItem(attrCount2);
                XmlNode tb_con = xmlDoc.CreateNode(XmlNodeType.Element, "Content", null);
                tb_con.InnerText = "TXT";//这两句是跟上面最主要的区别,这里是创建节点,并且是:XmlNodeType.Element类型

    tb.AppendChild(tb_con); //保存创建的XML文件 xmlDoc.Save(filepath);
    }

    /// <summary>
            /// 从XML文件获取数据
            /// </summary>
            /// <param name="spath">Xml文件路径</param>
           /// <param name="nodeName">要读取的节点</param>
           /// <returns>返回节点下的所有子节点数据DataTable</returns>
            public static DataTable ReadDefaultXml(string spath, string nodeName)
            {
                try
                {
                    DataSet ds = new DataSet();
                    if (File.Exists(spath))
                    {
                        ds.ReadXml(spath);
                    }
                    return ds.Tables[nodeName];
                }
                catch (Exception ex)
                {
                    //throw ex;
                    return null;
                }
            }

     以上的方法,直接调用:ReadDefaultXml(“Defa.xml”,"Label");//读取Label节点下的子节点。

  • 相关阅读:
    3.31上午英语视频
    3.30上午
    leetcode 38
    leetcode 36
    leetcode 28
    leetcode 27
    leetcode 26
    leetcode 24
    leetcode 21
    leetcode 20
  • 原文地址:https://www.cnblogs.com/hllxy/p/7919601.html
Copyright © 2011-2022 走看看