将节点添加到XMl

时间:2010-11-04 13:25:45

标签: c# xml dom namespaces appendchild

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");

唯一的问题是编写的New节点看起来像

<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:

我在做什么错误请...

1 个答案:

答案 0 :(得分:5)

你想:

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

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

在正确的命名空间中创建这些节点。