" <?xml version="1.0" encoding="utf-8" ?>
                         <book genre="fantasy" ISBN="2-3631-4">
                            <title>Oberon's Legacy</title>
                            <author>Corets, Eva</author>
                            <price>5.95</price>
                         </book>"
誰說上面的沒有跟節點啊﹐根節點就是<book>。

解决方案 »

  1.   

    用namespacemanager这个东西试试看.
      

  2.   

    xml文件的根节点之前再也不能插入节点
    但是你可以先把xml文件读到内存中,然后再用xmlTextWriter写回去,这样你可以在前面写任何东西了。至于名称空间:
          XmlDocument doc = new XmlDocument();
          doc.Load("booksort.xml");      //Create an XmlNamespaceManager for resolving namespaces.
          XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
          nsmgr.AddNamespace("bk", "urn:samples");
      

  3.   

    XmlDocument xmlDoc = new XmlDocument();
    XmlElement nodRoot;
    nodRoot = (XmlElement)xmlDoc.AppendChild(xmlDoc.CreateElement("你要加的根节点"));
      

  4.   

    try:XmlDocument doc=new XmlDocument();
    doc.Load(@"e:\test.xml");XmlElement ele=doc.CreateElement("bookstore");
    ele.SetAttribute("xmlns","http://tempuri.org/bookstore.xsd");
                    
    ele.AppendChild(doc.DocumentElement);
    doc.AppendChild(ele);doc.Save(@"e:\test.xml");
      

  5.   

    BearRui(孤熊 | 爱情!它把我给忘了!) ,真是太感谢你了!
      

  6.   

    再问一个小问题,如何判断doc.Load(@"e:\test.xml")中的test.xml文件是否以包含bookstore节点呢?
      

  7.   

    是不是这样判断:
          XmlNode nod=xmlDoc.SelectSingleNode("bookstore");
          if(nod == null)
         {
            .........................
          }
      

  8.   

    上面的可以,也可以这样:if(xmlDoc.DocumentElement.Name=="bookstore")
    {
    }
      

  9.   

    BearRui(孤熊 | 爱情!它把我给忘了!) 兄,正是不知道说什么感谢你,太激动了!