日期:2014-05-18  浏览次数:21024 次

100分悬赏 c# 生成xml
create table NS_CatalogTbl( --信息分类表
  CatalogID int not null, --标识
  CatalogName varchar(32) not null, --类别名
  CatalogDes varchar(255) null, --类别描述
  ParentID int null, -- 父类别标识
  UserID int not null,--创建用户
  IsShare char(1) null, --是否共享
  CatalogType char(1) null, --使用类型
  primary key(CatalogID)
)

表结构这样。
就是一个树形
用C#生成个树形的xml

------解决方案--------------------
我给你个思路,你用dataset试试,
先把表放到dataset 的表中,.net提供了生成xml的方法的吧,我也没有用过。
------解决方案--------------------
C# code

//先讀取資料到DataSet ds1
ds1.WriteXml(...)

------解决方案--------------------
很简单,用vs自带的 xsd工具即可生成强类型的dataset或者实体类的xml。
------解决方案--------------------
C# code
    public partial class Form4 : Form
    {
        private const string ConnectionString = "Integrated Security=SSPI;Initial Catalog=mydb;Data Source=localhost;";
        private FileStream fs;
        private XmlTextWriter w;
        private SqlConnection conn = new SqlConnection(ConnectionString);
        private SqlCommand cmd = null; 

        public Form4()
        {
            InitializeComponent();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fs = new FileStream(@"D:\NS_CatalogTbl.xml", FileMode.Create);
            w = new XmlTextWriter(fs, null);

            w.WriteStartDocument();
            w.WriteStartElement("NSCatalogTbl");
            w.WriteComment("This file generated for NS_CatalogTbl test.");

            CrateTreeXML(0);

            w.WriteEndElement();

            w.WriteEndDocument();
            w.Close();
        }

        private void CrateTreeXML(int ParentID)
        {
            DataSet dstm = GetData(ParentID);

            foreach(DataRow dr in dstm.Tables[0].Rows)
            {
                w.WriteStartElement("C" + ParentID.ToString());

                w.WriteAttributeString("CatalogID", "", dr["CatalogID"].ToString());
                w.WriteAttributeString("CatalogName", "", dr["CatalogName"].ToString());
                w.WriteAttributeString("CatalogDes", "", dr["CatalogDes"].ToString());
                w.WriteAttributeString("ParentID", "", dr["ParentID"].ToString());
                w.WriteAttributeString("UserID", "", dr["UserID"].ToString());
                w.WriteAttributeString("IsShare", "", dr["IsShare"].ToString());
                w.WriteAttributeString("CatalogType", "", dr["CatalogType"].ToString());
                CrateTreeXML(Convert.ToInt32(dr["CatalogID"]));

                w.WriteEndElement();
            }
        }

        private DataSet GetData(int ParentID)
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
            
            //conn.ConnectionString = ConnectionString;
            cmd = new SqlCommand("Select * from NS_CatalogTbl where ParentID= '"+ParentID.ToString().Trim()+"'", conn);

                conn.Open();
                SqlDataAdapter dataAdapter = new SqlDataAdapter();
                dataAdapter.SelectCommand = cmd;
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                return dataSet;               
        }

    }

------解决方案--------------------
DATASET哪个方法 不灵活.