提示root.AppendChild(xe1)这句未将对象引用设置到对象的实例XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath(XmlName));
XmlNode root=xmlDoc.SelectSingleNode("Replys");//查找<Replys>
XmlElement xe1=xmlDoc.CreateElement("Reply");//创建一个<Reply>节点XmlElement xesub1=xmlDoc.CreateElement("PostUserName");
xesub1.InnerText=author;//设置文本节点
xe1.AppendChild(xesub1);
XmlElement xesub2=xmlDoc.CreateElement("PostDateTime");
xesub2.InnerText=(DateTime.Now).ToString();
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("Content");
xesub3.InnerText=answer;
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
xmlDoc.Save(HttpContext.Current.Server.MapPath(XmlName));
xml文件
<?xml version="1.0"?>
<!--Created using an XML2005-5-23 10:11:27-->
<?xml-stylesheet type='text/xsl' href='list.xsl'?>
<Topic>
<Issue>
<TopicId>29</TopicId>
<PostUserName>客人</PostUserName>
<TopicName>trer</TopicName>
<ClassName>每日生活</ClassName>
<ReplyNum>0</ReplyNum>
<Pic>4</Pic>
<PostDateTime>2005-5-23 10:11:27</PostDateTime>
<Content>rreeee</Content>
</Issue>
<Replys />
</Topic>
xmlDoc.Load(HttpContext.Current.Server.MapPath(XmlName));
XmlNode root=xmlDoc.SelectSingleNode("Replys");//查找<Replys>
XmlElement xe1=xmlDoc.CreateElement("Reply");//创建一个<Reply>节点XmlElement xesub1=xmlDoc.CreateElement("PostUserName");
xesub1.InnerText=author;//设置文本节点
xe1.AppendChild(xesub1);
XmlElement xesub2=xmlDoc.CreateElement("PostDateTime");
xesub2.InnerText=(DateTime.Now).ToString();
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("Content");
xesub3.InnerText=answer;
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
xmlDoc.Save(HttpContext.Current.Server.MapPath(XmlName));
xml文件
<?xml version="1.0"?>
<!--Created using an XML2005-5-23 10:11:27-->
<?xml-stylesheet type='text/xsl' href='list.xsl'?>
<Topic>
<Issue>
<TopicId>29</TopicId>
<PostUserName>客人</PostUserName>
<TopicName>trer</TopicName>
<ClassName>每日生活</ClassName>
<ReplyNum>0</ReplyNum>
<Pic>4</Pic>
<PostDateTime>2005-5-23 10:11:27</PostDateTime>
<Content>rreeee</Content>
</Issue>
<Replys />
</Topic>
XmlNode root=xmlDoc.SelectSingleNode("Topic/Replys");//查找<Replys>