namespace createattribute
{
 /// <summary>
 /// class1 的摘要說(shuō)明。
 /// </summary>
 class class1
 {
  /// <summary>
  /// 應(yīng)用程序的主入口點(diǎn)。
  /// </summary>
  [stathread]
  static void main(string[] args)
  {
   //
   // todo: 在此處添加代碼以啟動(dòng)應(yīng)用程序
   //
   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 的摘要說(shuō)明。
 /// </summary>
 class class1
 {
  /// <summary>
  /// 應(yīng)用程序的主入口點(diǎn)。
  /// </summary>
  [stathread]
  static void main(string[] args)
  {
   //
   // todo: 在此處添加代碼以啟動(dòng)應(yīng)用程序
   //
   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
新聞熱點(diǎn)
疑難解答
圖片精選