首页 > 开发 > .Net > 正文

.Net FrameWork SDK文档的例子演示

2020-02-03 16:01:23
字体:
来源:转载
供稿:网友
xmldocument.createattribute 效果演示

using system;
using system.io;
using system.xml;

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表