.NET创建XML文件
来源:互联网 发布:网络棋牌赌博平台出租 编辑:程序博客网 时间:2024/05/16 09:22
创建一个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(); }}
- .NET创建XML文件
- c#.net创建xml文件
- VB.Net创建XML文件的方法
- asp.net下xml文件的创建
- .net创建XML文件的两种方法[转载]
- .net创建XML文件的两种方法
- asp.net创建XML文件的方法小结
- ASP.NET 创建XML
- ASP.NET 创建XML
- C#创建XML文件
- 创建XML文件
- java创建XML文件
- XmlTextWriter创建XML文件
- 创建XML文件
- 创建XML文件
- C#创建xml文件
- 创建xml文件
- C#创建Xml文件
- andorid 的shape用法
- Effective C++ Item 1:View C++ as a federation of languages
- aMule的命令行编译
- [入门扫盲]开发人员不能不知道的几个开发工具
- poj 3468 线段树 区间内的线段和(成段更新)
- .NET创建XML文件
- c 语言写的fastcgi 程序
- C#委托解析(收藏)
- Red Hat Enterprise 5中make xconfig命令不能使用的说明
- javascript 分模块架构
- PKU-静态成员作业-程设hw03
- 腾讯的产品管理之道
- JavaScript提升性能之事件委托
- 【redhat5.5】 samba服务器的配置