Persisting Custom dataColumns in XML

J

James Lungoss

Here's my scenario. I have a custom datacolumn class with added properties
and I need to persist this to xml. I know I can do it manually after the
dataset reads the xml but that means destroying the table and recreating it
on the fly and reimporting the row. It would be nice to have an override on
the creation of the columns when thge ds reads the xml.
Is there suh a thing?

The xml on the 1st write has the properties dumped
<xs:element name="Column1" msdata:MyStringProperty="String property"
msdata:MyBoolProperty="False" type="xs:string" minOccurs="0" />

but the properties are not there after reading and re-dumping. I know that
the ds must create standard datatables and datacolumns.

here's sample code

using System;

using System.Data;

namespace CustomColumn

{

/// <summary>

/// Summary description for Class1.

/// </summary>

class Class1

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{

//

// TODO: Add code to start application here

//

DataSet ds = new DataSet();

DataTable dt = new DataTable();

CustomColumn col = new CustomColumn();

col.ColumnName = "Column1";

col.DataType = Type.GetType("System.String");

col.MyStringProperty = "MySting1";

col.MyBoolProperty = true;



dt.Columns.Add(col);

col = new CustomColumn();

col.ColumnName = "Column2";

col.DataType = Type.GetType("System.String");

col.MyStringProperty = "MySting2";

col.MyBoolProperty = false;

dt.Columns.Add(col);

for(int i = 0; i<10;i++)

{

DataRow row = dt.NewRow();

row[0] = "row";

row[1] = i.ToString();

dt.Rows.Add(row);

}

ds.Tables.Add(dt);

ds.WriteXml("MyTables.xml",XmlWriteMode.WriteSchema);

ds.Tables.Clear();

ds.ReadXml("MyTables.xml",XmlReadMode.ReadSchema);

ds.WriteXml("MyTables_LostColumns.xml",XmlWriteMode.WriteSchema);





}

}

public class CustomColumn : DataColumn

{

public CustomColumn():base()

{

//

// TODO: Add constructor logic here

//

m_strMyProp = "String property";

m_bMyProp = false;

}

private string m_strMyProp;

private bool m_bMyProp;

public string MyStringProperty

{

get{return m_strMyProp;}

set{m_strMyProp = value;}

}

public bool MyBoolProperty

{

get{return m_bMyProp;}

set{m_bMyProp = value;}

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top