<?xml version="1.0" standalone="no"?>
<!--此文件表示Blog数据-->
<Blogs>
  <Blog ID="157">
    <User>
      <UserId>2104</UserId>
      <UserName>moodwu</UserName>
    </User>
    <SectionID>3</SectionID>
    <Subject>876</Subject>
    <Body>&lt;img height="19" src="/HRLog/Emoticons/sad_smile.gif" width="19" border="0" /&gt;</Body>
    <PostDate>2006-4-21 16:36:00</PostDate>
  </Blog>
  <Blog ID="156">
    <User>
      <UserId>2104</UserId>
      <UserName>moodwu</UserName>
    </User>
    <SectionID>3</SectionID>
    <Subject>09</Subject>
    <Body>&lt;img height="19" src="/Emoticons/shades_smile.gif" width="19" border="0" /&gt;</Body>
    <PostDate>2006-4-21 16:34:00</PostDate>
  </Blog>
</Blogs>
上面是我的XML文件,存储的是每个Blog主题的数据,现在我想将每条Blog对应的评论都写进来,期望得到这种格式的数据:
<?xml version="1.0" standalone="no"?>
<!--此文件表示Blog数据-->
<Blogs>
  <Blog ID="157">
    <User>
      <UserId>2104</UserId>
      <UserName>moodwu</UserName>
    </User>
    <SectionID>3</SectionID>
    <Subject>876</Subject>
    <Body>&lt;img height="19" src="/HRLog/Emoticons/sad_smile.gif" width="19" border="0" /&gt;</Body>
    <PostDate>2006-4-21 16:36:00</PostDate>
    <Comment CommentID="145">
    <CommentUser>
      <CommentUserId>2104</CommentUserId>
      <CommentUserName>moodwu</CommentUserName>
    </CommentUser>
    <CommentSubject>Just For Test</CommentSubject>
    <CommentBody>&lt;p&gt;My Test&lt;/p&gt; &lt;p&gt;&lt;img height="20" src="http://oa-taipei/Emoticons/QQ/45.gif" width="20" border="0" /&gt;&lt;img height="20" src="http://oa-taipei/Emoticons/hitwall.gif" width="25" border="0" /&gt;&lt;/p&gt;</CommentBody>
    <CommentPostDate>2006-4-21 11:18:00</CommentPostDate>
  </Comment>
  </Blog>
</Blogs>
在程序中我这样实现:
XPathNodeIterator ni = navigator.SelectDescendants("Blog", "", false);
XPathExpression expr = navigator.Compile("Blog[@ID='"+blogid+"']");
while( ni.MoveNext() )
{
XPathNavigator nav2 = ni.Current.Clone();
if (nav2.Matches(expr))
{
                     定义一个名为comment的XmlElement,其子Element分别为当前评论主题,内容,时间等.
然后通过这两条语句插入:
xmldocument.DocumentElement.AppendChild(comment);
xmldocument.Save(BlogXml);
这样的话,comment节点是一个跟Blog节点处于同样等级的节点,而不是我期望的显示在Blog节点里面.
我知道xmldocument.DocumentElement.AppendChild(comment)这样肯定是这种结果,但是我该怎么做才能得到期望的格式的数据呢?
谢谢
                   }