• 读取简单的xml


    XmlDocument doc = new XmlDocument();
    //加载要读取的XML
    doc.Load(@"F:Books.xml");
    //获得根节点
    XmlElement books = doc.DocumentElement;
    //获得子节点 返回节点的集合
    XmlNodeList xnl = books.ChildNodes;
    foreach (XmlNode item in xnl)
    {
        XmlElement xe = (XmlElement)item;
        Console.WriteLine(xe.GetAttribute("id"));
        
        XmlNodeList nodeList = xe.ChildNodes;
        foreach (XmlNode item2 in nodeList)
        {
            Console.WriteLine(item2.InnerText);
        }
    }
    Console.ReadKey();
    

      

    修改内容

    XmlDocument doc = new XmlDocument();
    doc.Load(@"F:Books.xml");
    XmlNodeList nodeList = doc.SelectSingleNode("/Books/Book[@id='3d310e87-6c46-4874-859e-c09f3acce589']").ChildNodes;
    foreach (XmlNode xn in nodeList)//遍历所有子节点
    {
        XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
        //Console.WriteLine(xe.GetAttribute("id"));
        if (xe.Name == "Price")
        {
            Console.WriteLine(xe.InnerText);
            xe.InnerText = "oooooooo";
        }
    }
    doc.Save(@"F:Books.xml");

    修改属性

    //改变属性的值
    XmlDocument doc = new XmlDocument();
    doc.Load("Order.xml");
    XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='190']");
    xn.Attributes["Count"].Value = "200";
    xn.Attributes["Name"].Value = "颜世伟";
    doc.Save("Order.xml");
    Console.WriteLine("保存成功");
  • 相关阅读:
    [levelDB] Version Manager
    [levelDB] Compaction
    <Effective Modern C++> 读书笔记
    <Effective Modern C++> 笔记-1-Deducing Type
    水塘抽样(Reservoir Sampling)问题
    【C++工程实践】条件变量
    【c++工程实践】内存模型
    【c++工程实践】智能指针
    【c++工程实践】值语义与右值引用
    分布式系统原理-CAP/2PC/3PC
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/12729138.html
Copyright © 2020-2023  润新知