繁体   English   中英

将节点添加到XMl

[英]Adding a node to 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");

唯一的事情是被写入的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:

我在做什么错了请...

你要:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM