<?xml version="1.0" encoding="utf-8" ?> 
- <root>
  <version>1.0</version> 
  <modified>2011-6-30 9:48:18</modified> 
  <seller_id>官网</seller_id> 
  <cat_url>http://baidu.com</cat_url> 
  <dir>http://baidu.com</dir> 
- <item_ids>
  <outer_id action="upload">ID1000018414</outer_id> 
  </item_ids>
  </root>我想在这基础上插入一条<outer_id action="upload">ID1000018414</outer_id> 
会的分享下,谢谢

解决方案 »

  1.   

    XML操作
    public static void Insert(string path, string node, string element, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    if (element.Equals(""))
                    {
                        if (!attribute.Equals(""))
                        {
                            XmlElement xe = (XmlElement)xn;
                            xe.SetAttribute(attribute, value);
                        }
                    }
                    else
                    {
                        XmlElement xe = doc.CreateElement(element);
                        if (attribute.Equals(""))
                            xe.InnerText = value;
                        else
                            xe.SetAttribute(attribute, value);
                        xn.AppendChild(xe);
                    }
                    doc.Save(path);
                }
                catch { }
            }
      

  2.   

    假定网站根目录下有test.xml文件
    XmlDocument xml = new XmlDocument();
    xml.Load(Server.MapPath("~/test.xml"));
    XmlNode item_ids = xml.GetElementsByTagName("item_ids")[0];
    XmlElement newElement = xml.CreateElement("outer_id");
    newElement.SetAttribute("action", "upload");
    newElement.InnerText = "ID1000018414";
    item_ids.AppendChild(newElement);
    xml.Save(Server.MapPath("~/test.xml"));
      

  3.   

    XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("~/etao/IncrementIndex.xml"));        XmlNode objRootNode = xmlDoc.SelectSingleNode("root");
            XmlElement objChildNode = xmlDoc.CreateElement("item_ids");
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = xmlDoc.CreateElement("outer_id");
            objElement.InnerText = "lucky";
            objChildNode.AppendChild(objElement);
            xmlDoc.Save(Server.MapPath("~/etao/IncrementIndex.xml"));结果:
     <?xml version="1.0" encoding="utf-8" ?> 
    - <root>
      <version>1.0</version> 
      <modified>2011-6-30 9:48:18</modified> 
      <seller_id>官网</seller_id> 
      <cat_url>http:///etao/SellerCats.xml</cat_url> 
      <dir>http:///etao/item/</dir> 
    - <item_ids>
      <outer_id action="upload">ID1000018414</outer_id> 
      </item_ids>
    - <item_ids>
      <outer_id>lucky</outer_id> 
      </item_ids>
      </root>
    如果XmlNode objRootNode = xmlDoc.SelectSingleNode("root");
    换成XmlNode objRootNode = xmlDoc.SelectSingleNode("item_ids");就报错