• XML操作


    下载路径

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace XMLOperation
    {
        class Program
        {
            //XPath获取xml节点
            static void Main(string[] args)
            {
                //CreateXml(@"D:TestBooks.xml");
    
                string path = @"....upload";
                string[] filenames = Directory.GetFiles(path); //获取该文件夹下面的所有文件名
                foreach (var item in filenames)
                {
                    ReadXml(item);
                }
    
    
            }
    
            private static void CreateXml(string filePath)
            {
                //1、创建XML文档对象
                XmlDocument doc = new XmlDocument();
    
                //2、创建第一个行描述信息,并且添加到doc文档中
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
    
                //3、创建根节点,将根节点添加到文档中
                XmlElement root = doc.CreateElement("Books");
                doc.AppendChild(root);
    
                //4-1、给根节点创建子节点book1,将book1添加到根节点
                XmlElement book1 = doc.CreateElement("Book");
                root.AppendChild(book1);
                //4-2、给book1添加子节点
                XmlElement book1Name = doc.CreateElement("Name");
                book1Name.InnerText = "肖申克的救赎";
                XmlElement book1Price = doc.CreateElement("Price");
                book1Price.InnerText = "36.0";
                XmlElement book1Author = doc.CreateElement("Author");
                book1Author.InnerText = "斯蒂芬金";
                book1.AppendChild(book1Name);
                book1.AppendChild(book1Price);
                book1.AppendChild(book1Author);
    
                //5-1、给根节点创建子节点book2,将book2添加到根节点
                XmlElement book2 = doc.CreateElement("Book");
                root.AppendChild(book2);
                //5-2、给book2添加子节点
                XmlElement book2Name = doc.CreateElement("Name");
                book2Name.InnerText = "白夜行";
                XmlElement book2Price = doc.CreateElement("Price");
                book2Price.InnerText = "24.0";
                XmlElement book2Author = doc.CreateElement("Author");
                book2Author.InnerText = "东野圭吾";
                book2.AppendChild(book2Name);
                book2.AppendChild(book2Price);
                book2.AppendChild(book2Author);
                //5-3、给节点book2添加属性——SetAttribute
                book2.SetAttribute("Id", "24");
                book2.SetAttribute("Count", "16");
    
                //6、保存xml到指定目录,默认会覆盖原文件
                doc.Save(filePath);
    
                Console.WriteLine("创建{0}成功", filePath);
            }
    
            private static void ReadXml(string filePath)
            {
                //1、创建XML文档对象
                XmlDocument doc = new XmlDocument();
    
                //2、加载指定路径的XML
                doc.Load(filePath);
    
                //3、获得根节点
                XmlElement root = doc.DocumentElement;
    
                //4-1、获得根节点的所有子节点
                //XmlNodeList allNodes = root.ChildNodes;
                //foreach (XmlNode node in allNodes)
                //{
                //    Console.WriteLine(node.InnerText);
                //}
    
                //4-2、获得某一级别的节点集合
                //XmlNodeList booksList = doc.SelectNodes("/Books/Book");
                //foreach (XmlNode node in booksList)
                //{
                //    Console.WriteLine(node.InnerText);
                //}
    
                //4-3、通过属性值获得单个指定的节点,并改变某个属性的值
                XmlNodeList cols = doc.SelectNodes("Config/Table/Column");
    
    
    
    
    
                //Console.WriteLine(baiYeXing.InnerText);
                //Console.WriteLine(baiYeXing.Attributes["Count"].Value);
                //baiYeXing.Attributes["Count"].Value = "9";
                //Console.WriteLine(baiYeXing.Attributes["Count"].Value);
                foreach (XmlNode item in cols)
                {
                    if (item.Attributes["HeaderText"] != null)
                    {
                        string HeaderText = item.Attributes["HeaderText"].Value;
                        XmlAttribute FlagName = doc.CreateAttribute("FlagName");
                        FlagName.Value = HeaderText;
                        item.Attributes.Append(FlagName);
                    }
                }
                doc.Save(filePath);
            }
        }
    }

    https://share.weiyun.com/898239fbc0e5d4358b4de56c1ab8265d

  • 相关阅读:
    AMD 开源照片级渲染引擎 Radeon ProRender
    删除集群mds
    删除集群mds
    一次旅途她和豆浆结缘,如今拥有70多家加盟店
    小伙居然开网店卖花,整整一年时间他做出了400万元的业绩
    带着800元现金开始创业,居然开出了十几家连锁超市
    北大学霸从小米离职,靠眼镜打开亿万市场
    广西农民靠养猪发家致富,采用新模式既有效益又保护生态
    在母婴产品领域他独领风骚,利润已经突破了1亿
    80后小伙白手起家做照明生意,为他创造了六千万元的业绩
  • 原文地址:https://www.cnblogs.com/gaocong/p/6600771.html
Copyright © 2020-2023  润新知