我在数据集中读取XML。以下是我的XML结构。
XML: -
<?xml version="1.0" encoding="utf-8" ?>
<root>
<ClaimHomePage>
<item>
<controlID>txtHospitalName</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
<item>
<controlID>txtHospitalAddress</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
<item>
<controlID>txtAuthorizeAmount</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
<item>
<controlID>txtDtTmOfAdmission</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
</ClaimHomePage>
<ClaimBillDetailHC>
<item>
<controlID>txtHospitalName</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
<item>
<controlID>txtHospitalAddress</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
<item>
<controlID>txtAuthorizeAmount</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
<item>
<controlID>txtDtTmOfAdmission</controlID>
<rolesEnabled>Non Medico,Medico,Auditor,CMO</rolesEnabled>
<rolesVisible>Non Medico,Medico,Auditor,CMO</rolesVisible>
</item>
</ClaimBillDetailHC>
</root>
enter code here
主根节点内有两个根节点(即ClaimHomePage,ClaimBillDetailHC)。
我想写一个代码,这样当我从这个XML创建数据集时,数据集中应该有两个表(即ClaimHomePage&amp; ClaimBillDetailHC)。
请提供一些帮助......
答案 0 :(得分:1)
使用Relations
集合。我认为这是获取表实例的直接方式。
DataSet ds = new DataSet();
ds.ReadXml(file);
if (ds.Relations.Count == 2)
{
DataTable first = ds.Relations[0].ChildTable;
DataTable second = ds.Relations[0].ChildTable;
Console.WriteLine("Table : " + ds.Relations[0].ParentTable.TableName);
foreach (DataRow row in first.Rows)
Console.WriteLine(row["controlID"] + " " + row["rolesEnabled"] + " " + row["rolesVisible"]);
}
您必须使用DOM或Linq to XML API阅读XML
文档,然后填写DataSet / DataTable。
例如(Linq to XML)
XDocument doc = XDocument.Load(file);
//List from ClaimHomePage
var ClaimHomePage = from ele in doc.Root.Element("ClaimHomePage").Descendants("item")
select new
{
ControlID=(string)ele.Element("controlID"),
rolesEnabled = (string)ele.Element("rolesEnabled"),
rolesVisible = (string)ele.Element("rolesVisible"),
};
var ClaimBillDetailHC= from ele in doc.Root.Element("ClaimBillDetailHC").Descendants("item")
select new
{
ControlID=(string)ele.Element("controlID"),
rolesEnabled = (string)ele.Element("rolesEnabled"),
rolesVisible = (string)ele.Element("rolesVisible"),
};
然后遍历列表并填充DataTable / DataSet。