• 推荐
  • 评论
  • 收藏

C# 生成XML

2022-11-09    3126次浏览

代码下载

表结构:

USE [NewHyeySaleData]
GO
/****** 对象:  Table [dbo].[YwHyDrugCategory]    脚本日期: 03/26/2011 18:35:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[YwHyDrugCategory](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[ProductName] [nvarchar](50) NULL,
	[Component] [nvarchar](50) NULL,
	[Dosageform] [nvarchar](50) NULL,
	[FirstCategory] [nvarchar](50) NULL,
	[FunctionSys] [nvarchar](50) NULL,
	[SecondCategory] [nvarchar](50) NULL,
	[MajorFunction] [nvarchar](255) NULL,
	[ExclusiveProduct] [tinyint] NULL,
	[InsuranceProduct] [tinyint] NULL,
	[PrimaryProduct] [tinyint] NULL,
	[OTC] [nvarchar](5) NULL,
	[ProtectProduct] [tinyint] NULL,
 CONSTRAINT [PK_YwHyDrugCategory] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'产品名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'ProductName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成份' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'Component'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'剂型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'Dosageform'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'大类' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'FirstCategory'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'治疗系统' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'FunctionSys'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'亚类' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'SecondCategory'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'功能主治' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'MajorFunction'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'独家品种 1是0否' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'ExclusiveProduct'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'医保药物  1是0否' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'InsuranceProduct'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'基本药物 1是0否' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'PrimaryProduct'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'OTC 0否' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'OTC'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'中药保护品种 1是0否' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'YwHyDrugCategory', @level2type=N'COLUMN',@level2name=N'ProtectProduct'

数据:

中药	妇科用药    妇科洗液
中药	补益药	      补血药
中药	补益药	      补血药
中药	补益药      补血药
中药	补益药	     补血药
中药	补益药	     补血药
中药	补益药     补血药
中药	补益药	     补血药
中药	补益药     补血药
中药	补益药     补血药
中药	补益药	     补血药
中药	补益药	     补血药
中药	补益药	     补血药

查询:

select FirstCategory from YwHyDrugCategory group by FirstCategory

select  FunctionSys from YwHyDrugCategory where FirstCategory = '化学药品' group by FunctionSys

select  FunctionSys from YwHyDrugCategory where FirstCategory = '中药' group by FunctionSys



select SecondCategory from YwHyDrugCategory where FirstCategory = '中药' and FunctionSys='中药注射剂'  group by SecondCategory 

C#:

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.Data.SqlClient;
using System.Xml;

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

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //建立连接,将表名及对应表明的字段放入dataset中,
                string strcon = "Data Source=192.168.1.2;Initial Catalog=NewHyeySaleData;Persist Security Info=True;User ID=sa;Password=hyey-20060705;Connect Timeout=3000;";
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = strcon;
                    if (con.State == ConnectionState.Closed)
                        con.Open();
                    SqlCommand cmd;
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    this.label1.Text = "生成药品分类XML文件";

                    //FirstCategory
                    string strFirst = "select FirstCategory from YwHyDrugCategory group by FirstCategory";
                    cmd = new SqlCommand(strFirst, con);
                    da.SelectCommand = cmd;
                    da.Fill(ds, "FirstCategory");

                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        XmlDocument xmldoc = new XmlDocument();
                        XmlElement root = xmldoc.CreateElement("YwHyDrugCategory");
                        xmldoc.AppendChild(root);
                        foreach (DataRow item in ds.Tables[0].Rows)
                        {

                            XmlElement FirstCategory = xmldoc.CreateElement("FirstCategory");
                            XmlAttribute fname = xmldoc.CreateAttribute("name");
                            fname.Value = item["FirstCategory"].ToString();
                            FirstCategory.Attributes.Append(fname);
                            root.AppendChild(FirstCategory);

                            string strSecond = "select  FunctionSys from YwHyDrugCategory where FirstCategory = '" + item["FirstCategory"].ToString().Trim() + "' group by FunctionSys";
                            cmd = new SqlCommand(strSecond, con);
                            da.SelectCommand = cmd;
                            DataSet SecondDs = new DataSet();
                            da.Fill(SecondDs, "FunctionSys");
                            if (SecondDs != null && SecondDs.Tables[0].Rows.Count > 0)
                            {
                                foreach (DataRow row in SecondDs.Tables[0].Rows)
                                {
                                    XmlElement FunctionSys = xmldoc.CreateElement("FunctionSys");
                                    XmlAttribute sname = xmldoc.CreateAttribute("name");
                                    sname.Value = row["FunctionSys"].ToString();
                                    FunctionSys.Attributes.Append(sname);
                                    FirstCategory.AppendChild(FunctionSys);

                                    string strThird = "select  SecondCategory from YwHyDrugCategory where FirstCategory = '" + item["FirstCategory"].ToString().Trim() + "'and FunctionSys='" + row["FunctionSys"].ToString().Trim() + "'  group by SecondCategory";
                                    cmd = new SqlCommand(strThird, con);
                                    da.SelectCommand = cmd;
                                    DataSet ThirdDs = new DataSet();
                                    da.Fill(ThirdDs, "SecondCategory");

                                    if (ThirdDs != null && ThirdDs.Tables[0].Rows.Count > 0)
                                    {
                                        foreach (DataRow rowt in ThirdDs.Tables[0].Rows)
                                        {
                                            XmlElement SecondCategory = xmldoc.CreateElement("SecondCategory");
                                            XmlAttribute tname = xmldoc.CreateAttribute("name");
                                            tname.Value = rowt["SecondCategory"].ToString();
                                            SecondCategory.Attributes.Append(tname);
                                            FunctionSys.AppendChild(SecondCategory);
                                        }
                                        ThirdDs.Dispose();
                                        ThirdDs.Clear();
                                    }

                                }

                                SecondDs.Dispose();
                                SecondDs.Clear();
                            }

                        }
                        ds.Dispose();
                        ds.Clear();
                        da.Dispose();
                        xmldoc.Save(@"E:\生成药品分类XML文件.xml");
                        this.label1.Text += "  XML文件路径-E:生成药品分类XML文件.xml";
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                ;
            }
            
           
        }
    }
}
原文地址:https://www.cnblogs.com/Leo_wl/p/1996451.html