C# xml文件读取节点写入数据

   

<enums>
  <catagory id="SalesOrderType">
    <value description="订单">0</value>
    <value description="退货单">1</value>
  </catagory>

  <catagory id="SalesOrderStatus">
    <value description="已拆分">-30</value>
    <value description="已合并">-20</value>
    <value description="已撤销">-10</value>
    <value description="未确认">0</value>
    <value description="已确认">10</value>
    <value description="已审核">20</value>
    <value description="已打印">30</value>
    <value description="已取货">40</value>
    <value description="已验货">50</value>
    <value description="已包装">60</value>
    <value description="已发货">70</value>
    <value description="已核销">80</value>
  </catagory>

 </enums>     

XmlDocument xmldoc = new XmlDocument();
                //xmldoc.Load(Directory.GetCurrentDirectory() + "\\enums.xml"); //Debug调试 
                xmldoc.Load(Path.Combine(Application.StartupPath,"enums.xml"));           
                XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;   //得到顶层节点列表
                foreach (XmlElement element in topM)
                {
                    if (element.Name.ToLower() == "catagory")
                    {
                        string catagory = element.Attributes["id"].Value;
                        XmlNodeList nodelist = element.ChildNodes;  //得到该节点的子节点
                        if (nodelist.Count > 0)
                        {
                            foreach (XmlElement el in nodelist) //读元素值
                            {                                                              
                                int keyint =int.Parse(el.InnerText);
                                string value= el.Attributes["description"].Value;
                                tenum=new Tenum(catagory,keyint,value);
                                //插入数据
                            }
                        }
                    }
                }    


       

你可能感兴趣的:(xml,String,C#,enums)