zoukankan      html  css  js  c++  java
  • 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"); 
  • 相关阅读:
    spring mvc注解文件上传下载
    html,图片上传预览,input file获取文件等相关操作
    three.js、webGL、canvas区别于关联
    html添加新元素兼容和访问
    关于HTML,css3自适应屏幕,自适应宽度
    数据库设计的规则 入门
    mysql 索引入门
    一 .linux上安装 python git redis nginx
    一 .git和github
    一 .Django+Alipay(支付宝支付使用)和微信支付
  • 原文地址:https://www.cnblogs.com/chinabc/p/2518154.html
Copyright © 2011-2022 走看看