网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 asp.net简单生成XML文件的方法
内容
    本文实例讲述了asp.net简单生成XML文件的方法。分享给大家供大家参考,具体如下:
    方式一:直接使用DataSet
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Server=127.0.0.1;User ID=sa;Password=sa;Database=northwind;Persist Security Info=True";
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter("select * from 表", conn);
    SqlCommandBuilder thisBulder = new SqlCommandBuilder(da);
    DataSet ds = new DataSet();
    da.Fill(ds);
    ds.WriteXml(@"C:/temp.xml");
    方式二:自定义生成方式
    using System.Xml;//头部加此命名空间
    XmlDocument xd = new XmlDocument();//表示XML文档
    XmlDeclaration xde;//表示 XML 声明节点:<?xml version='1.0'...?>
    xde = xd.CreateXmlDeclaration("1.0", "GBK", null);//参数的第二项为编码方式
    //standalone定义了是否可以在不读取任何其它文件的情况下处理该文档,默认为no
    xd.AppendChild(xde);//<?xml version="1.0" encoding="UTF-8" standalone="yes"?>生成结束
    XmlElement xe = xd.CreateElement("Root");//创建一个Root根元素
    xd.AppendChild(xe);//Root根元素创建完成
    XmlNode root = xd.SelectSingleNode("Root");//查找<Root>
    XmlElement xe1 = xd.CreateElement("Tree");//在<Root>之下创建元素<Tree>
    xe1.SetAttribute("id","1");//指定属性的属性值
    xe1.InnerText = "类型1";//指定属性文本节点
    root.AppendChild(xe1);//完成子节点<Tree>
    xd.Save(Server.MapPath("xml.xml"));
    希望本文所述对大家asp.net程序设计有所帮助。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/18 8:35:03