创建XML文件的两种方法

 创建XML文件的两种方法

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0cm; margin-bottom:.0001pt; text-align:center; mso-pagination:none; tab-stops:center 207.65pt right 415.3pt; layout-grid-mode:char; border:none; mso-border-bottom-alt:solid windowtext .75pt; padding:0cm; mso-padding-alt:0cm 0cm 1.0pt 0cm; font-size:9.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:1.0cm 1.0cm 1.0cm 1.0cm; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->

//注意 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
>

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

 

你可能感兴趣的:(创建XML文件的两种方法)