namespace createattribute
{
/// <summary>
/// class1 的摘要说明。
/// </summary>
class class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main(string[] args)
{
//
// todo: 在此处添加代码以启动应用程序
//
xmldocument doc = new xmldocument();
doc.loadxml("<book genre='novel' isbn='1-861001-57-5'>" +
"<title>pride and prejudice</title>" +
"</book>");
//create an attribute.
xmlattribute attr = doc.createattribute("publisher");
attr.value = "worldwide publishing";
//add the new node to the document.
doc.documentelement.setattributenode(attr);
console.writeline("display the modified xml...");
doc.save(console.out);
}
}
}
效果如下:
display the modified xml...
<?xml version="1.0" encoding="gb2312"?>
<book genre="novel" isbn="1-861001-57-5" publisher="worldwide publishing">
<title>pride and prejudice</title>
</book>press any key to continue
xmldocument.createnode 方法效果演示
using system;
using system.xml;
namespace createnode
{
/// <summary>
/// class1 的摘要说明。
/// </summary>
class class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main(string[] args)
{
//
// todo: 在此处添加代码以启动应用程序
//
xmldocument doc = new xmldocument();
doc.loadxml("<book>" +
" <title>oberon's legacy</title>" +
" <price>5.95</price>" +
"</book>");
// create a new element node.
xmlnode newelem;
newelem = doc.createnode(xmlnodetype.element, "pages", "");
newelem.innertext = "290";
console.writeline("add the new element to the document...");
xmlelement root = doc.documentelement;
root.appendchild(newelem);
console.writeline("display the modified xml document...");
console.writeline(doc.outerxml);
}
}
}
效果:
add the new element to the document...
display the modified xml document...
<book><title>oberon's legacy</title><price>5.95</price><pages>290</pages></book>
press any key to continue
新闻热点
疑难解答
图片精选