【问题标题】:Adding a node to XMl将节点添加到 XML
【发布时间】:2010-11-04 13:25:45
【问题描述】:

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:

请问我做错了什么...

【问题讨论】:

    标签: c# xml dom namespaces appendchild


    【解决方案1】:

    你想要:

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

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

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

    【讨论】:

    • 不,我让它们处于正确的位置......但我没有得到
    • 我把它们放在正确的位置,但我得到的结果是
    • 嗨,尼克,非常感谢,它现在可以工作了.. 但是空的 xmlns 现在已经移动到下一个标题节点:(
    • @Nick Jones:+1 这是一个正确的答案。 @Bikram:你检查了吗?重置命名空间声明是因为您在 null(或空)命名空间 URI 中构建一个元素。
    • @Bikram:您应该对非空命名空间 URI 下的每个元素使用此方法。
    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多