网上找到了一个示例
data.xml的文件,内容如下,
<?xml version="1.0" encoding="gb2312"?>
<Employees>
  <Node genre="李赞红" ISBN="2-3631-4">
    <title>CS从入门到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="李赞红" ISBN="2-3631-4">
    <title>CS从入门到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
</Employees>添加一个结点:XmlDocument xmlDoc=new XmlDocument(); 
xmlDoc.Load(Server.MapPath("data.xml")); 
XmlNode root=xmlDoc.SelectSingleNode("Employees");//查找<Employees> 
XmlElement xe1=xmlDoc.CreateElement("Node");//创建一个<Node>节点 
xe1.SetAttribute("genre","张三");//设置该节点genre属性 
xe1.SetAttribute("ISBN","1-1111-1");//设置该节点ISBN属性 XmlElement xesub1=xmlDoc.CreateElement("title"); 
xesub1.InnerText="C#入门帮助";//设置文本节点 
xe1.AppendChild(xesub1);//添加到<Node>节点中 
XmlElement xesub2=xmlDoc.CreateElement("author"); 
xesub2.InnerText="高手"; 
xe1.AppendChild(xesub2); 
XmlElement xesub3=xmlDoc.CreateElement("price"); 
xesub3.InnerText="158.3"; 
xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<Employees>节点中 
xmlDoc.Save ( Server.MapPath("data.xml") );这样是可行的.但如果我的data.xml为以下内容就会出错,请问如果是像下面的XML如何添加?
也就是<Employees> 属性多了个suxing="abc"
<?xml version="1.0" encoding="gb2312"?>
<Employees suxing="abc">
  <Node genre="李赞红" ISBN="2-3631-4">
    <title>CS从入门到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="李赞红" ISBN="2-3631-4">
    <title>CS从入门到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
</Employees>

解决方案 »

  1.   

    XmlDocument d = new XmlDocument();
                XmlElement root = d.CreateElement("Employees");
                root.SetAttribute("suxing", "abc");
                d.AppendChild(root);
      

  2.   

    能否详细点。本人初次接触XML
    我想每添加节点就变成以下格式
    <Employees suxing="abc"> 
       <Node genre="李赞红" ISBN="2-3631-4"> 
         <title>CS从入门到精通 </title> 
         <author>候捷 </author> 
         <price>58.3 </price> 
       </Node> 
       <Node genre="李赞红" ISBN="2-3631-4"> 
         <title>CS从入门到精通 </title> 
         <author>候捷 </author> 
         <price>58.3 </price> 
       </Node> 
       <Node genre="abc" ISBN="2-3631-4"> 
         <title>aaa</title> 
         <author>bbb</author> 
         <price>ccc</price> 
       </Node> 
       <Node genre="cde" ISBN="2-3631-4"> 
         <title>ccc</title> 
         <author>123</author> 
         <price>4321</price> 
       </Node> 
     
    ...
    </Employees> 
      

  3.   

     主要是这个对节点增加属性root.SetAttribute("suxing", "abc");
      

  4.   

    像有些多属性的怎么办?就比如用
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"     
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
      <url>
        <loc>http://www.abc.com/</loc>
        <priority>0.5</priority>
        <lastmod>2008-03-11T08:45:45+00:00</lastmod>
        <changefreq>always</changefreq>
      </url>
      <url>
        <loc>http://www.ced.com/aaa</loc>
        <priority>1.0</priority>
        <lastmod>2008-04-02T02:22+08:00</lastmod>
        <changefreq>always</changefreq>
      </url>
    </urlset>这种怎么处理?
      

  5.   

    多属性就多用几次root.SetAttribute("..", "..");
      

  6.   

    我用的是NET1.1的.好像没有这个方法