日期:2014-05-18 浏览次数:21224 次
//先讀取資料到DataSet ds1 ds1.WriteXml(...)
------解决方案--------------------
很简单,用vs自带的 xsd工具即可生成强类型的dataset或者实体类的xml。
------解决方案--------------------
    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哪个方法 不灵活.