• NET下XML的读写操作


    使用Xml.Linq
    XML 格式:
    <?xml version="1.0" encoding="utf-8"?>
    <SalaryMap>
      <Salary unid="821B26E9FBA51DC9482579CD002811FE" name="青岛山东路营业部2012年2月营销人员薪酬申请表">
        <File>营销人员薪酬申请明细表.xls</File>
        <SalaryTypeName>01营销人员薪酬申请表</SalaryTypeName>
        <SalaryType>1</SalaryType>
        <Month>201202</Month>
        <BarchID></BarchID>
        <BarchName>青岛山东路营业部</BarchName>
      </Salary>

    ......

    </SalaryMap>

     

    1. 写操作
    private void SaveXmlMapData(List<SalaryInfo> salaryList)
            {
                XElement root = new XElement("SalaryMap");//XElement.Parse("<SalaryMap></SalaryMap>");
                
    //var root = doc.Element("SalaryMap");
                foreach (var item in salaryList)
                {
                    root.Add(new XElement("Salary",
                        new XAttribute("unid", item.UNID),
                        new XAttribute("name", item.Name),
                        new XElement("File", item.FileName),
                        new XElement("SalaryTypeName",item.SalaryTypeName),
                        new XElement("SalaryType", (int)item.SalaryType),
                        new XElement("Month", item.Month),
                        new XElement("BarchID", item.BarchID),
                        new XElement("BarchName",item.BarchName)
                        ));
                }
                root.Save(SalaryMapFilePath);
            }
    2.读取数据
    public List<SalaryInfo> LoadXmlMapData()
            {
                List<SalaryInfo> salaryList = new List<SalaryInfo>();
                var xmlReader = new XmlTextReader(SalaryMapFilePath);
                if (xmlReader.ReadToDescendant("SalaryMap"))
                {
                    var node = XDocument.Parse(xmlReader.ReadOuterXml()).Element("SalaryMap").Elements("Salary");
                    foreach (XElement item in node)
                    {
                        salaryList.Add(new SalaryInfo()
                        {
                            UNID=item.Attribute("unid").Value,
                            Name = item.Attribute("name").Value,
                            SalaryType = (SalaryTypeOptions)Enum.Parse(typeof(SalaryTypeOptions), item.Element("SalaryType").Value),
                            SalaryTypeName=item.Element("SalaryTypeName").Value,
                            FileName=item.Element("File").Value,
                            Month = item.Element("Month").Value,
                            BarchID = item.Element("BarchID").Value,
                            BarchName = item.Element("BarchName").Value
                        });
                    }
                }
                return salaryList;
            }
    3. 替换/修改/删除 节点值
    XElement root = XElement.Parse(@"  
                                       <Categories>  
                                          <Category>  
                                            <CategoryID>1</CategoryID>  
                                            <CategoryName>Beverages</CategoryName>  
                                            <Description>Soft drinks, coffees, teas, beers, and ales</Description>  
                                          </Category>  
                                        </Categories>  
                                      
    ");
                //替换节点
                root.Element("Category").Element("CategoryID").ReplaceWith(new XElement("ID""2"));
                //设置节点值
                root.Element("Category").SetElementValue("CategoryName""test data");
                //移除节点
                root.Element("Category").Element("Description").Remove();
                //添加节点
                root.Element("Category").Element("CategoryID").Add(new XElement("Desc"new XCData("中文test/")));
                Console.WriteLine(root.ToString());
                root.Save("test.xml"); 
  • 相关阅读:
    Python--BeautifulSoup4丶Tag丶Xpath丶requests+re的基础学习及使用
    c#字符串字面量
    vim操作
    序列的方法
    python数值类型与序列类型
    Linux操作学习笔记1
    Jav的10个面向对象设计原则
    JAVA面向对象基础
    二进制 八进制 十六进制
    using 的故事
  • 原文地址:https://www.cnblogs.com/chinabc/p/2518154.html
Copyright © 2020-2023  润新知