zoukankan      html  css  js  c++  java
  • XML使用与总结

    xml是一种比较方便的数据储存方式,它适用于小数据的存储。最常见的适用地方莫过于各种web.config与app.config了。
     
    一、创建一个简单的xml路径
     public static string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "test.xml";
    二、创建一个xml文件,并添加一级数据【只有一级数据】
            public void Set()
            {
                try
                {
                    XmlDocument xmldoc = new XmlDocument();
                    if (!File.Exists(xmlPath))
                    {
                        //不关闭会有错误提示,被另一个线程占用
                        File.Create(xmlPath).Close();
    
                        //创建root节点
                        XmlElement root = xmldoc.CreateElement("Root");
                        xmldoc.AppendChild(root);
                        xmldoc.Save(xmlPath);
                    }
    
                    //加载xml文件
                    xmldoc.Load(xmlPath);
                    var rootElement = xmldoc.DocumentElement;
    
                    //创建子节点
                    var childnode = xmldoc.CreateElement("Phone");
                    //设置属性
                    childnode.SetAttribute("Name", "IPhone 6s");
                    childnode.SetAttribute("BackGround", "银白色 玫瑰色 土豪金");
                    //设置内容
                    childnode.InnerText = "6s 已经是淘汰产品了";
    
                    //追加到Root节点
                    rootElement.AppendChild(childnode);
                    //保存XML
                    xmldoc.Save(xmlPath);
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

    执行结果,执行了3次,每次添加一些属性

    <Root>
      <Phone Name="IPhone 6s" />
      <Phone Name="IPhone 6s">6s 已经是淘汰产品了</Phone>
      <Phone Name="IPhone 6s" BackGround="银白色 玫瑰色 土豪金">6s 已经是淘汰产品了</Phone>
    </Root>

     三、创建有2级数据的xml文件,一级节点的名字最好保持一致,否则查询时有困难。

            public void Set()
            {
                try
                {
                    XmlDocument xmldoc = new XmlDocument();
                    if (!File.Exists(xmlPath))
                    {
                        //不关闭会有错误提示,被另一个线程占用
                        File.Create(xmlPath).Close();
    
                        //创建root节点
                        XmlElement root = xmldoc.CreateElement("Root");
                        xmldoc.AppendChild(root);
                        xmldoc.Save(xmlPath);
                    }
    
                    //加载xml文件
                    xmldoc.Load(xmlPath);
                    var rootElement = xmldoc.DocumentElement;
    
                    //创建一级节点
                    var childnode = xmldoc.CreateElement("HuaWeiPhone");
                    childnode.SetAttribute("Name", "华为手机");
    
                    //创建二级节点
                    var hwbg = xmldoc.CreateElement("BackGround");
                    hwbg.SetAttribute("Description", "颜色");
                    hwbg.InnerText = "白色 黑色 玫瑰色 金色";
                    childnode.AppendChild(hwbg);
    
                    //创建二级节点
                    var hwprice = xmldoc.CreateElement("Price");
                    hwprice.SetAttribute("Description", "价格");
                    hwprice.InnerText = "2000";
                    childnode.AppendChild(hwprice);
                    //追加到Root节点
                    rootElement.AppendChild(childnode);
    
    
                    //创建一级节点
                    var childnode2 = xmldoc.CreateElement("XiaoMiPhone");
                    childnode2.SetAttribute("Name", "小米手机");
                    //创建二级节点
                    var hwbg2 = xmldoc.CreateElement("BackGround");
                    hwbg2.SetAttribute("Description", "颜色");
                    hwbg2.InnerText = "白色 黑色 ";
                    childnode2.AppendChild(hwbg2);
    
                    //创建二级节点
                    var hwprice2 = xmldoc.CreateElement("Price");
                    hwprice2.SetAttribute("Description", "价格");
                    hwprice2.InnerText = "1500";
                    childnode2.AppendChild(hwprice2);
    
    
                    //追加到Root节点
                    rootElement.AppendChild(childnode2);
                    //保存XML
                    xmldoc.Save(xmlPath);
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

    xml文件为:

    <Root>
      <HuaWeiPhone Name="华为手机">
        <BackGround Description="颜色">白色 黑色 玫瑰色 金色</BackGround>
        <Price Description="价格">2000</Price>
      </HuaWeiPhone>
      <XiaoMiPhone Name="小米手机">
        <BackGround Description="颜色">白色 黑色 </BackGround>
        <Price Description="价格">1500</Price>
      </XiaoMiPhone>
    </Root>

    四、清理全部的xml数据

            public void Delete()
            {
                try
                {
                    if (!File.Exists(xmlPath)) return;
                    var xmldoc = new XmlDocument();
                    xmldoc.Load(xmlPath);
                    XmlNodeList xn = xmldoc.GetElementsByTagName("Root");
                    if(xn?.Count<=0)return;
    
                    //加载root节点
                    var rootElement = xmldoc.DocumentElement;
                    rootElement.RemoveAll();
                    xmldoc.AppendChild(rootElement);
                    xmldoc.Save(xmlPath);
    
                }
                catch (Exception e)
                {
                }
            }

    五、根据节点名字,清理某一条数据

            public void DeleteByName(string name = "HuaWeiPhone")
            {
                try
                {
                    if (!File.Exists(xmlPath)) return;
    
                    XElement root = XElement.Load(xmlPath);
                    var element = root.Element(name);
                    element.Remove();
                    root.Save(xmlPath);
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }

     六、获取数据,根据某一个节点查询

                    XmlDocument xmldoc = new XmlDocument();
                    //加载xml文件
                    xmldoc.Load(xmlPath);
    
                    //方法一、根据节点名字获取节点信息
                    XmlNode xmlNode = xmldoc.SelectSingleNode("Root/XiaoMiPhone");//XiaoMiPhone一级节点名字
                    //该节点的属性
                    var name = xmlNode.Attributes["Name"].Value;
                    XmlNodeList childNodes = xmlNode.ChildNodes;
                    foreach (XmlNode item in childNodes)
                    {
                        //节点属性
                        var bgdescription = item.Attributes["Description"].Value;
                        //节点内容
                        var desTxt = item.InnerText;
                        //节点名字
                        var nodeName = item.Name;
                    }

     七、遍历数据,xml数据如下

    <Root>
      <Phone Name="Oppo手机">
        <BackGround Description="颜色">白色 黑色 玫瑰色 金色</BackGround>
        <Price Description="价格">2000</Price>
      </Phone>
      <Phone Name="Vivo手机">
        <BackGround Description="颜色">白色 黑色 </BackGround>
        <Price Description="价格">1500</Price>
      </Phone>
      <Phone Name="小米手机">
        <BackGround Description="颜色">白色 黑色 </BackGround>
        <Price Description="价格">1800</Price>
      </Phone>
      <Phone Name="锤子手机">
        <BackGround Description="颜色">银色 黑色 酒红 金色 </BackGround>
        <Price Description="价格">2200</Price>
      </Phone>
    </Root>

     获取xml数据的方法

    可以根据根据节点的名字查询数据,要是输入一级节点,就是查询全部的,要是输入二级节点怎么会获取所有二级节点的该节点数据
    XmlNodeList xmlNodeList = xmldoc.SelectNodes("Root/Phone/BackGround");
            public void Get()
            {
                try
                {
                    XmlDocument xmldoc = new XmlDocument();
                    //加载xml文件
                    xmldoc.Load(xmlPath);
    
                    //查询全部数据
                    XmlNodeList xmlNodeList = xmldoc.SelectNodes("Root/Phone");
                    if (xmlNodeList == null || xmlNodeList.Count <= 0) return;
    
                    foreach (XmlNode item in xmlNodeList)
                    {
                        //节点属性
                        var bgdescription = item.Attributes["Name"].Value;
                        foreach (XmlNode citem in item.ChildNodes)
                        {
                            var bgDes = citem.Attributes["Description"].Value;
                            //节点内容
                            var desTxt = citem.InnerText;
                            //节点名字
                            var nodeName = citem.Name;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
  • 相关阅读:
    [转]《三国演义》人名英文翻译妙评 你也来评评!
    Effective C++:改善程序技术与设计思维的55个有效做法(第三版)(中文版)(预订)Effective C++ 3RD
    A Closer Look At Parallax Occlusion Mapping
    [转]收录全部作品,《寂静岭套装》发售开始
    [转]程序员版《桃花庵》
    Cg 1.5 Beta 2
    FilteredTextBox控件
    CollapsiblePanel控件
    DynamicPopulate控件
    HoverMenu控件的使用
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/3945882.html
Copyright © 2011-2022 走看看