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"); 

 

 

posted @ 2012-05-25 15:46  Aleax  阅读(259)  评论(0编辑  收藏  举报