.NET创建XML文件

时间:2022-01-09 21:19:53

 创建一个WebSite网站,在CS文件中写入代码,如下:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected XmlDocument doc;

protected void button_OnClick(object sender, EventArgs e)
{
doc = new XmlDocument();
//加入XML的声明段落,<?xml version="1.0" encoding="GB2312"?>
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0","GB2312",null);
//添加新节点到末尾
doc.AppendChild(dec);//AppendChild方法为XmlNode的方法

//加入一个根元素
XmlElement ele = doc.CreateElement("html1");
doc.AppendChild(ele);

//加入另外一个元素
for (int i = 0; i < 3; i++)
{
//查找<Employees>节点
XmlNode root = doc.SelectSingleNode("html1");
XmlElement ele1 = doc.CreateElement("body1");
ele1.SetAttribute("id","body1");

XmlElement ele1_1 = doc.CreateElement("div");
ele1_1.InnerText = "你好!";
ele1.AppendChild(ele1_1);
XmlElement ele1_2 = doc.CreateElement("div");
ele1_2.InnerText = "加油!";
ele1.AppendChild(ele1_2);

root.AppendChild(ele1);

}
doc.Save(Server.MapPath("data.xml"));

string content = System.IO.File.ReadAllText(Server.MapPath("data.xml"));
Response.Write(content);
}

}

注:XmlDocument类中的方法CreateXmlDeclaration返回XmlDeclaration(创建一个新的XmlDeclaration对象)

public virtual XmlDeclaration CreateXmlDeclaration(string version, string encoding, string standalone)
{
return new XmlDeclaration(version, encoding, standalone, this);
}


以上为第一种创建XML方式,同时可以用System.xml.xmlWriter类来创建

WriteStartDocument  当在派生类中被重写时,编写版本为“1.0”的 XML 声明。 

WriteCData  当在派生类中被重写时,写出包含指定文本的 <![CDATA[...]]> 块。

WriteStartElement(String)  当在派生类中被重写时,写出具有指定的本地名称的开始标记。

WriteEndElement  当在派生类中被重写时,关闭一个元素并弹出相应的命名空间范围。

WriteEndDocument  当在派生类中被重写时,关闭任何打开的元素或特性并将编写器重新设置为 Start 状态。

WriteEndAttribute  当在派生类中被重写时,关闭上一个 WriteStartAttribute 调用。  

WriteAttributeString(String, String)  当在派生类中被重写时,写出具有指定的本地名称和值的特性。

下面为运用部分(MSDN文档)

using System;

using System.IO;

using System.Xml;


public class Sample {


private const string filename = "sampledata.xml";


public static void Main() {


XmlWriterSettings settings = new XmlWriterSettings();

settings.Indent = true;

XmlWriter writer = XmlWriter.Create(filename, settings);


// Write the Processing Instruction node.

String PItext="type=\"text/xsl\" href=\"book.xsl\"";

writer.WriteProcessingInstruction("xml-stylesheet", PItext);


// Write the DocumentType node.

writer.WriteDocType("book", null , null, "<!ENTITY h \"hardcover\">");


// Write a Comment node.

writer.WriteComment("sample XML");


// Write the root element.

writer.WriteStartElement("book");


// Write the genre attribute.

writer.WriteAttributeString("genre", "novel");


// Write the ISBN attribute.

writer.WriteAttributeString("ISBN", "1-8630-014");


// Write the title.

writer.WriteElementString("title", "The Handmaid's Tale");


// Write the style element.

writer.WriteStartElement("style");

writer.WriteEntityRef("h");

writer.WriteEndElement();


// Write the price.

writer.WriteElementString("price", "19.95");


// Write CDATA.

writer.WriteCData("Prices 15% off!!");


// Write the close tag for the root element.

writer.WriteEndElement();


writer.WriteEndDocument();


// Write the XML to file and close the writer.

writer.Flush();

writer.Close();

}


}