小编Bik*_*ram的帖子

将节点添加到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>
Run Code Online (Sandbox Code Playgroud)

我需要添加一个书籍节点..我的代码是这样的

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 …
Run Code Online (Sandbox Code Playgroud)

c# xml dom namespaces appendchild

1
推荐指数
1
解决办法
1619
查看次数

标签 统计

appendchild ×1

c# ×1

dom ×1

namespaces ×1

xml ×1