惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - yunshu

(转帖)C/S、B/S及三层结构漫谈 TP-Link 配置(转帖) 如何用VSS VS2005重置按钮代码 asp.net 2.0 网站导航控件 WEB自定义控件小记 VS2005 DataGridView 和 GirdView 横向大比拼 web.config中连接字符串与数据库登陆方式的联系 转帖-win2003各版本的区别 您没有调试该服务器的权限,验证您是服务器“Debugger Users”组的成员 错误:未将对象引用设置到对象的实例 在绑定DataGrid控件,做添加删除时遇到问题(数组越界) visual studio.net已检测到指定的web服务器运行的不是asp.net1.1版。 request('id')语句,返回的是什么类型的数据 [WebMethod] 是什么意思? 如何在DataSet中追加记录 怎样添加本地web引用 枚举类型是什么意思,怎么用? 什么是DOM?
创建XML文件的两种方法
yunshu · 2008-03-23 · via 博客园 - yunshu

来自博客园“小二哥博客”
//注意 using System.Xml;

方法一:按照XML的结构一步一步的构建XML文档.
    通过.Net FrameWork SDK中的命名空间"System.Xml"中封装的各种类来实现的
    XmlText xmltext ;
    XmlDocument xmldoc = new XmlDocument( ) ;
 
    //加入XML的声明段落
    XmlNode xmlnode =xmldoc.CreateXmlDeclaration("1.0","gb2312",null);
    xmldoc.AppendChild ( xmlnode ) ;
 
    //加入一个根元素
    XmlElement xmlelem = xmldoc.CreateElement ( "" , "bookstore" , "" ) ;
    xmltext = xmldoc.CreateTextNode ( "" ) ;
    xmlelem.AppendChild ( xmltext ) ;
    xmldoc.AppendChild ( xmlelem ) ;
    
    //加入一个子元素
    XmlElement xmlelem1 = xmldoc.CreateElement ( "" , "book" , "" ) ;
    xmltext = xmldoc.CreateTextNode ( "" ) ;
    xmlelem1.AppendChild (xmltext) ;
    //为子元素"book"增加两个属性
    xmlelem1.SetAttribute("genre","","fantasy");
    xmlelem1.SetAttribute("ISBN","2-3631-4");

    xmldoc.ChildNodes.Item(1).AppendChild (xmlelem1) ;
    
    //创建三个子元素的子元素
    XmlElement  xmlelem2 = xmldoc.CreateElement ( "" , "title" , "" ) ;
    xmltext = xmldoc.CreateTextNode ( "Oberon's Legacy" ) ;
    xmlelem2.AppendChild ( xmltext ) ;
    xmldoc.ChildNodes.Item(1).AppendChild ( xmlelem1 ).AppendChild(xmlelem2) ;
 
    XmlElement xmlelem3 = xmldoc.CreateElement ( "" , "author" , "" ) ;
    xmltext = xmldoc.CreateTextNode ( "Corets, Eva" ) ;
    xmlelem3.AppendChild ( xmltext ) ;
    xmldoc.ChildNodes.Item(1).AppendChild ( xmlelem1 ).AppendChild(xmlelem3) ;
 
    XmlElement xmlelem4 = xmldoc.CreateElement ( "" , "price" , "" ) ;
    xmltext = xmldoc.CreateTextNode ( "5.95" ) ;
    xmlelem4.AppendChild ( xmltext ) ;
    xmldoc.ChildNodes.Item(1).AppendChild ( xmlelem1 ).AppendChild(xmlelem4) ;
    xmldoc.Save ( @"C:\Inetpub\wwwroot\MyAspnet\bin\bookstore.xml") ; //保存

方法二:直接定影XML文档,然后保存到文件。
        通过"XmlDocument"类中的"LoadXml"方法
    XmlDocument xmldoc = new XmlDocument ( ) ; //创建空的XML文档
    xmldoc.LoadXml("<?xml version='1.0' encoding='gb2312'?>"+
     "<bookstore>"+
     "<book genre='fantasy' ISBN='2-3631-4'>" +
     "<title>Oberon's Legacy</title>" +
     "<author>Corets, Eva</author>"+
     "<price>5.95</price>"+
     "</book>"+
     "</bookstore>");
    xmldoc.Save ( @"C:\Inetpub\wwwroot\MyAspnet\bin\bookstore.xml") ; //保存

=======================以上两种方法产生如下的XML文档===========================
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
  <book genre="fantasy" ISBN="2-3631-4">
    <title>Oberon's Legacy</title>
    <author>Corets, Eva</author>
    <price>5.95</price>
  </book>
</bookstore
>

比较:第一种创建起来更加灵活,而第二种创建起来更加方便。