当前位置:编程学习 > C#/ASP.NET >>

c#生成xml文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Operationcs per = new Operationcs();
            DataTable dt = per.Select();
  XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load("new.xml");
            }
            catch
            {
                XmlTextWriter xtw = new XmlTextWriter("new.xml", Encoding.UTF8);   //新建XML文件
                xtw.WriteStartDocument();
                xtw.WriteStartElement("Deskbooks");                 // gnode根节点
                xtw.WriteEndElement();
                xtw.WriteEndDocument();
                xtw.Close();
                doc.Load("new.xml");                
            }

            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                DataRow dr = dt.Rows[i];
                XmlNode xn = doc.DocumentElement;                 //  找到根节点
                XmlElement xE = doc.CreateElement("deskbook");
                XmlElement xe = doc.CreateElement("Title");         //    在根节点下创建元素,如果是属性,则用XmlAttribute;
                xe.InnerText = "Juvenile Law";                                      //       给子节点写入文本节点(值)
                xE.AppendChild(xe);                                          //  根节点将其纳入
                xn.AppendChild(xE);

                XmlElement Edition = doc.CreateElement("Edition");
                Edition.InnerText = "Juvenile Law 2011";
                xE.AppendChild(Edition);
                xn.AppendChild(xE);

                XmlElement Supplement = doc.CreateElement("Supplement");
                Supplement.InnerText = "False";
                xE.AppendChild(Supplement);
                xn.AppendChild(xE);

                XmlElement Author = doc.CreateElement("Author");
                Author.InnerText = dr["id"].ToString();
                xE.AppendChild(Author);
                xn.AppendChild(xE);

                XmlElement ChapterCaption = doc.CreateElement("ChapterCaption");
                ChapterCaption.InnerText = dr["caption"].ToString();
                xE.AppendChild(ChapterCaption);
                xn.AppendChild(xE);

                XmlElement ChapterName = doc.CreateElement("ChapterName");
                ChapterName.InnerText = "The Juvenile Office";
                xE.AppendChild(ChapterName);
                xn.AppendChild(xE);

                XmlElement ChapterContent = doc.CreateElement("ChapterContent");
                ChapterContent.InnerText = "";
                xE.AppendChild(ChapterContent);
                xn.AppendChild(xE);

                XmlElement SectionCaption = doc.CreateElement("SectionCaption");
                SectionCaption.InnerText = "Section 1";
                xE.AppendChild(SectionCaption);
                xn.AppendChild(xE);

                XmlElement SectionName = doc.CreateElement("SectionName");
                SectionName.InnerText = "Introduction";
                xE.AppendChild(SectionName);
                xn.AppendChild(xE);

                XmlElement SectionContent = doc.CreateElement("SectionContent");
                SectionContent.InnerText = "Procedure in Juvenile and Family Court Divisions of the Circuit";
                xE.AppendChild(SectionContent);
                xn.AppendChild(xE);

                XmlElement EditionDate = doc.CreateElement("EditionDate");
                EditionDate.InnerText = "";
                xE.AppendChild(EditionDate);
                xn.AppendChild(xE);

                XmlElement RevisionDate = doc.CreateElement("RevisionDate");
                RevisionDate.InnerText = "";
                xE.AppendChild(RevisionDate);
                xn.AppendChild(xE);

                XmlElement ShortName = doc.CreateElement("ShortName");
                ShortName.InnerText = " Juvenile Law 2011 - Chapter 1.1";
                xE.AppendChild(ShortName);
                xn.AppendChild(xE);

                XmlElement LibrarySourceConst = doc.CreateElement("LibrarySourceConst");
                LibrarySourceConst.InnerText = "MODESKBOOKS";
                xE.AppendChild(LibrarySourceConst);
                xn.AppendChild(xE);
            }
            doc.Save("new2.xml");                        //  利用XmlDocument保存文件
    
            MessageBox.Show("ok");
        }
    }     
}
--------------------编程问答-------------------- 分享?请问还是有什么问题 --------------------编程问答-------------------- 保存一下,没什么问题。谢谢!!! --------------------编程问答-------------------- 可能是那设置有问题,你可以加我CSDN为好友再加QQ好友,
我给你传个实例程序,你一看就会了 --------------------编程问答-------------------- 呵呵,来这个发分享帖子了。顶你。 --------------------编程问答-------------------- 原来是分享 --------------------编程问答--------------------   哇哇哇··我太喜欢你了···虽然我用不上,但是我会保留下来,我某些项目刚好需要这个东西,你懂的··· --------------------编程问答-------------------- 請問一下  Operationcs per = new Operationcs();
這個是要引用哪個Function、開啟資料庫?  或是需要加入參考來源呢?  --------------------编程问答-------------------- 有东西分享,楼主好样的!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,