.NET创建XML文件

4 篇文章 0 订阅

 创建一个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();  
  
}


}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值