XML to DataTable
I have tried various methods listed in responses here using stream, ReadXml, etc. and I'm getting the same results (no rows added). I'm fairly certain it is something that is wrong with my DataTable's structure.
DataTable code (contents of function that builds the structure and returns it):
DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Access1");
dt.Columns.Add("Access2");
dt.Columns.Add("Access3");
dt.Columns.Add("Access4");
dt.Columns.Add("Access5");
dt.TableName = "Permission";
return dt;
I have tried it with setting typeof(string) in the Add parameters (to match the schema), but that didn't help it.
DataTable schema:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Permission" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Permission">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" type="xs:string" minOccurs="0" />
<xs:element name="Access1" type="xs:string" minOccurs="0" />
<xs:element name="Access2" type="xs:string" minOccurs="0" />
<xs:element name="Access3" type="xs:string" minOccurs="0" />
<xs:element name="Access4" type="xs:string" minOccurs="0" />
<xs:element name="Access5" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
</NewDataSet>
XML going in:
<?xml version="1.0" encoding="utf-16"?>
<Permissions>
<Permission>
<Item>Process 1</Item>
<Access1>True</Access1>
<Access2>True</Access2>
<Access3>False</Access3>
<Access4>False</Access4>
<Access5>False</Access5>
</Permission>
<Permission>
<Item>Process 2</Item>
<Access1>True</Access1>
<Access2开发者_如何学运维>True</Access2>
<Access3>True</Access3>
<Access4>False</Access4>
<Access5>False</Access5>
</Permission>
</Permissions>
I have a feeling it has to do with my table structure, but I'm unsure where it is incorrect.
Thanks for any help you can give
Attach the DataTable
to a DataSet
, and XML serialize the dataset. DataSet
is xml serializable; DataTable
is not.
Sample program to show how this works:
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Access1");
dt.Columns.Add("Access2");
dt.Columns.Add("Access3");
dt.Columns.Add("Access4");
dt.Columns.Add("Access5");
dt.TableName = "Permission";
for (int i = 0; i < 6; i++)
{
var row = dt.NewRow();
row["Item"] = i;
row["Access1"] = (i % 2 == 0 ? true : false);
row["Access2"] = (i % 2 == 0 ? true : false);
row["Access3"] = (i % 3 == 0 ? true : false);
row["Access4"] = (i % 3 == 0 ? true : false);
row["Access5"] = (i % 4 == 0 ? true : false);
dt.Rows.Add(row);
}
dt.AcceptChanges();
DataSet ds = new DataSet("Permissions");
ds.Tables.Add(dt);
var output = Console.OpenStandardOutput();
ds.WriteXml(output);
Console.ReadLine();
}
}
With code4life's explanation above about needing to have it in a DataSet, I put it into action like this:
DataTable table = CreatePermissionTable(); // uses the c# code listed in question
DataSet ds = new DataSet();
ds.Tables.Add(table);
try
{
using(Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
ds.ReadXml(stream);
table = ds.Tables["Permission"];
return table;
}
}
精彩评论