C# 向XMl添加节点
XMLC# 向XMl添加节点,c#,xml,dom,namespaces,appendchild,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 xmln
<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>
唯一要做的是写入的新节点看起来像
<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>
61小时
李
小孩
6.99
***
测试手册
比克拉姆
曼恩
2.99
***
它有一个额外的XMLNS=“”,并且节点中缺少g:
请问我做错了什么 您想要:
System.Xml.XmlElement XmlElementbook =
doc.CreateElement("book","http://www.contoso.com/books");
及
要在正确的名称空间中创建这些节点,请执行以下操作:
System.Xml.XmlElement XmlElementbook =
doc.CreateElement("book","http://www.contoso.com/books");
及
在正确的名称空间中创建这些节点。否,我正在将它们放置在正确的位置。。但是我不明白,我正在把它们放在正确的位置上,但是我得到了结果,阿希·尼克非常感谢你,它现在起作用了。。但是空的xmlns现在已经移动到下一个titles节点:(@Nick Jones:+1这是一个正确的答案。@Bikram:您检查了吗?重置命名空间声明是因为您正在null(或空)中构建一个元素命名空间URI。@Bikram:您应该对非空命名空间URI下的每个元素使用此方法。不,我将它们放置在正确的位置。但是我没有将它们放置在正确的位置,但是我得到了结果。非常感谢您,它正在工作。但是空的xmlns现在已移动到下一个标题节点:(@Nick Jones:+1这是一个正确的答案。@Bikram:您检查了吗?重置命名空间声明是因为您正在null(或空)命名空间URI中生成一个元素。@Bikram:您应该对非null命名空间URI下的每个元素使用此方法。
XmlElementbook.SetAttribute("genre","http://www.contoso.com/genre","novel5");