修改现有xml节点的值

XmlDocument doc1 = new XmlDocument();

                doc1.Load(Server.MapPath("~/etao/IncrementIndex.xml"));

                //修改商品

                XmlNodeList nodes1 = doc1.GetElementsByTagName("outer_id");

                bool ismodif = false;

                foreach (XmlNode xn in nodes1)//遍历任何子节点 

                {

                    XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 

                    if (xe.InnerText.Equals(pid))

                    {

                        xe.SetAttribute("action", "upload");//则修改该属性为“delete”

                        XmlNodeList modified = doc1.GetElementsByTagName("modified");

                        modified.Item(0).InnerText = DateTime.Now.ToString();

                        doc1.Save(Server.MapPath("~/etao/IncrementIndex.xml"));


xml:
  
    
 <?xml version="1.0" encoding="utf-8" ?> 

- <root>

  <version>1.0</version> 

  <modified>2011-7-4 9:42:54</modified> 

  <seller_id>官网</seller_id> 

  <cat_url>etao/SellerCats.xml</cat_url> 

  <dir>/etao/item/</dir> 

- <item_ids>

  <outer_id action="upload">ID1000018452</outer_id> 

  <outer_id action="upload">ID1000018413</outer_id> 

  </item_ids>

  </root>


你可能感兴趣的:(xml)