.net创建XML文件的两种方法[转载]

方法一:按照XML的结构一步一步的构建XML文档.
    通过.Net FrameWork SDK中的命名空间"System.Xml"中封装的各种类来实现的

方法二:直接定影XML文档,然后保存到文件。
        通过"XmlDocument"类中的"LoadXml"方法

 

.aspx前台代码:

< %@ Page Language ="C#" AutoEventWireup ="true" CodeFile ="Default4.aspx.cs" Inherits ="Default4" % >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
     < title > </title>
</head>
< body >
     < form id ="form1" runat ="server" >
     < div >
     < asp:Button ID ="btn" runat ="server" OnClick ="btn1_OnClick" Text ="创建xml的第一种方法" /> < br />
     < asp:Button ID ="btn2" runat ="server" OnClick ="btn2_OnClick" Text ="创建xml的第二种方法" />
     </div>
     </form>
</body>
</html>

.cs后台代码实现如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml; 

 
public partial class Default4 : System.Web.UI.Page

    protected void Page_Load(object sender, EventArgs e) 
    
 
    }
 
    //创建xml文件方法一 
    protected void btn1_OnClick(object sender, EventArgs e) 
    
        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(Server.MapPath("bookstore.xml")); //保存 
    }
 
 
    //创建xml文件方法二 
    protected void btn2_OnClick(object sender, EventArgs e) 
    
        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(Server.MapPath("bookstore2.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 >

你可能感兴趣的:(.net创建XML文件的两种方法[转载])