2010-11-04 282 views
1

的XML將節點添加到XML

<bookstore xmlns="http://www.contoso.com/books" 
      xmlns:g="http://www.contoso.com/genre"> 
    <book g:genre="novel" publicationdate="2010-03-01" ISBN="1-123456-15-0"> 
    <title>61 Hours</title> 
    <author xmlns="http://www.contoso.com/author"> 
     <first-name>Lee</first-name> 
     <last-name>Child</last-name> 
    </author> 
    <price>6.99</price> 
    </book> 
<bookstore> 

我需要一本書節點添加到它。我的代碼讀取這樣的

strpath = "C:\\BookStore.xml"; 
XmlDocument doc = new XmlDocument(); 
doc.Load(strpath); 
XmlNode root = doc.DocumentElement; 
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable); 
nsMgr.AddNamespace("b", "http://www.contoso.com/books"); 
nsMgr.AddNamespace("g", "http://www.contoso.com/genre"); 
nsMgr.AddNamespace("a", "http://www.contoso.com/author"); 
// Create a Book element and populate its attributes 
System.Xml.XmlElement XmlElementbook = doc.CreateElement("book"); 
//create the three attributes to hold the values 
XmlElementbook.SetAttribute("g:genre";"novel5"); 
XmlElementbook.SetAttribute("publicationdate", "2010-11-03"); 
XmlElementbook.SetAttribute("ISBN", "1-00000-00-00"); 
// Insert the new element into the XML tree 
// Create a new XML element and populate its attributes 
System.Xml.XmlElement myXmlElementTitle = doc.CreateElement("title"); 
myXmlElementTitle.InnerText = "TestBook"; 
// Insert the new element under the node we created 
XmlElementbook.AppendChild(myXmlElementTitle); 
System.Xml.XmlElement myXmlElementAuthor = doc.CreateElement("author"); 
myXmlElementAuthor.SetAttribute("xmlns", ("http://www.contoso.com/author")); 
System.Xml.XmlElement myXmlElementFirstname = doc.CreateElement("first-name"); 
myXmlElementFirstname.InnerText = "Bikram"; 
myXmlElementAuthor.AppendChild(myXmlElementFirstname); 
System.Xml.XmlElement myXmlElementLastname = doc.CreateElement("last-name"); 
myXmlElementLastname.InnerText = "Mann"; 
myXmlElementAuthor.AppendChild(myXmlElementLastname); 
XmlElementbook.AppendChild(myXmlElementAuthor); 
// Price 
System.Xml.XmlElement myXmlElementPrice = doc.CreateElement("price"); 
myXmlElementPrice.InnerText = "2.99"; 
// Insert the new element under the node we created 
XmlElementbook.AppendChild(myXmlElementPrice); 
//append the whole node to file 
doc.DocumentElement.AppendChild(XmlElementbook); 
doc.Save("C:\\BookStore.xml"); 

的唯一的事情是,被寫入新節點貌似

<bookstore xmlns="http://www.contoso.com/books" 
      xmlns:g="http://www.contoso.com/genre"> 
     <book g:genre="novel" publicationdate="2010-03-01" ISBN="1-123456-15-0"> 
     <title>61 Hours</title> 
     <author xmlns="http://www.contoso.com/author"> 
      <first-name>Lee</first-name> 
      <last-name>Child</last-name> 
     </author> 
     <price>6.99</price> 
     </book> 

    ***<book genre="novel5" 
      publicationdate="2010-11-03" 
      ISBN="1-00000-00-00" 
      xmlns=""> 
    <title>TestBook</title> 
    <author xmlns="http://www.contoso.com/author"> 
     <first-name>Bikram</first-name> 
     <last-name>Mann</last-name> 
    </author> 
    <price>2.99</price> 
    </book>*** 
    <bookstore> 

它有一個額外的XMLNS = 「」 和g:缺少在節點

我究竟做錯了,請...

回答

5

你想:

System.Xml.XmlElement XmlElementbook = 
    doc.CreateElement("book","http://www.contoso.com/books"); 

XmlElementbook.SetAttribute("genre","http://www.contoso.com/genre","novel5"); 

建立在正確的命名空間這些節點。

+0

不,我得到他們在正確的位置..但我沒有得到 – Bikram 2010-11-04 13:36:46

+0

我讓他們在正確的位置,但我得到的結果爲 – Bikram 2010-11-04 13:37:20

+0

嗨尼克非常感謝你,它現在工作..但空xmlns已經移動到下一個標題節點:( – Bikram 2010-11-04 13:50:28