简体   繁体   English

强类型数据集-XML序列化

[英]Strongly typed dataset - XML Serialization

I have a typed dataset in my project and i need to import data from XML. 我的项目中有一个类型化的数据集,我需要从XML导入数据。 I managed to import data to Tables collection in my typed dataset, but I need to have access to the data via Annotations - properties named the same as tables in my dataset. 我设法将数据导入类型化数据集中的Tables集合中,但是我需要能够通过Annotations(与我的数据集中的表命名相同的属性)访问数据。

this.ConfigDataSet.Reset();
this.ConfigDataSet.ReadXml(FileName,XmlReadMode.ReadSchema);
this.ConfigDataSet.AcceptChanges();

Do I have to copy data field by field? 我是否必须逐字段复制数据?

I have something like this: 我有这样的事情:

this.ConfigDataSet.Tables["TabName"].Rows.Count //returns 3 
this.ConfigDataSet.TabName.Rows.Count //returns 0

I write xml like this: 我这样写xml:

this.ConfigDataSet.AcceptChanges();
this.ConfigDataSet.WriteXml(FileName,XmlWriteMode.WriteSchema);

Check this out: 看一下这个:

class Program
{
    static void Main(string[] args)
    {
        DataTable tbl = GetTable();
        DataSet ds = new DataSet();
        ds.Tables.Add(tbl);
        //file path to write xml file
        ds.WriteXml("test.xml");

        DataSet ds2 = new DataSet();
        //file path to read xml file
        ds2.ReadXml("test.xml");
    }
    static DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        table.Rows.Add(25, "Indocin", "David", DateTime.Now);
        table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
        table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
        table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
        table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
        return table;
    }
}

Check here for DataSet.WriteXml() and here for DataSet.ReadXml() ; 在这里检查DataSet.WriteXml()在这里检查DataSet.ReadXml() ;

Solution: 解:

WriteXml: WriteXml:

                ConfigDataSet.WriteXml(FileName);

ReadXml: ReadXml:

                    using (DataSet ds = new DataSet())
                {
                    ds.ReadXml(FileName);
                    ConfigDataSet.Load(ds.Tables[0].CreateDataReader(), LoadOption.OverwriteChanges, ConfigDataSet.TableName1);
                    ConfigDataSet.Load(ds.Tables[1].CreateDataReader(), LoadOption.OverwriteChanges, ConfigDataSet.TableName2);
                    ConfigDataSet.Load(ds.Tables[2].CreateDataReader(), LoadOption.OverwriteChanges, ConfigDataSet.TableName3);
                    ConfigDataSet.Load(ds.Tables[3].CreateDataReader(), LoadOption.OverwriteChanges, ConfigDataSet.TableName4);
                }

It is required to load each table separately, but it is better than load each field row by row. 需要单独加载每个表,但是比逐行加载每个字段更好。

Regards, Kuba. 问候,库巴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM