<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type='text/xsl' href='/expert/Xsl/2.xsl'?>
<Topic>
<Issue>
<PostUserNickName></PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>       100</credit>
<TopicId>   5039179</TopicId>
<TopicName>求GridView读取xml文件的源码</TopicName>
<PostUserId>1186149</PostUserId>
<PostUserName>hcl123</PostUserName>
<RoomName>.NET技术 ASP.NET</RoomName>
<ReplyNum>2</ReplyNum>
<PostDateTime>2006-09-21 18:02:58</PostDateTime>
<Point>0</Point>
<ReadNum>0</ReadNum>
<RoomId>5202</RoomId>
<EndState>0</EndState>
<Content>求GridView读取xml文件的源码,通过xpath来查询,绑定符合条件的xml记录!!
多谢各位了!!</Content>
</Issue>
<Replys>
</Replys>
</Topic>
请问现在如何向Replys节点添加子节点?

解决方案 »

  1.   

    添加一个结点: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") );//////////////////////////////////////////////////////////////////////////////////////
    结果:在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>
      <Node genre="张三" ISBN="1-1111-1">
        <title>C#入门帮助</title>
        <author>高手</author>
        <price>158.3</price>
      </Node>
    </Employees>