[英]Create XML from a hierarchical Dataset in C#
我有一個數據集,我使用SQL中的Recursion創建,
Parent UserId Child Reporting_To_UserId Depth id
Aditya 13 Abhishek 4 0 13
Abhishek 4 Saurabh 6 1 16
Abhishek 4 Mohinder 8 1 17
Mohinder 8 Mohammad 14 2 18
Saurabh 6 Rahul 1 2 11
Saurabh 6 Amitesh 5 2 12
現在我想生成一個看起來像這樣的XML: -
<Person name="Aditya" User_Id="13">
<Person name="Abhishek" User_Id="4">
<Person name="Mohinder" User_id="8">
<Person name="Mohammad" User_id="14"/>
</Person>
<Person name="Saurabh" User_Id="6">
<Person name="Rahul" User_Id="1"/>
<Person name="Amitesh" User_Id="5"/>
</Person>
</Person>
</Person>
我想使用數據集中的父和子關系創建一個Hierarchical XML。
我認為它可以用遞歸LINQ完成,但我仍然需要弄清楚如何正確編寫它,所以這里有一個帶遞歸方法的解決方案:
首先,你聲明方法(我已經按Name
搜索,但你也可以用Id
進行搜索):
public static IEnumerable<XElement> BuildXML(string Parent, DataTable dt)
{
string filter = string.Format("[Parent] = '{0}'", Parent);
return from x in dt.Select(filter)
select new XElement("Person",
new XAttribute("Name", x["Child"]),
new XAttribute("User_Id", x["Reporting_To_UserId"]),
BuildXML(x["Child"].ToString(), dt)
);
}
然后,用你的父元素調用它(我添加了一行,否則查詢會更復雜):
var dt = new DataTable();
dt.Columns.AddRange(new[] {
new DataColumn("Parent"),
new DataColumn("UserId"),
new DataColumn("Child"),
new DataColumn("Reporting_To_UserId"),
new DataColumn("Depth"),
new DataColumn("id")
});
dt.Rows.Add(new object[] { "", 0, "Aditya", 13, 0, 12 });
dt.Rows.Add(new object[] {"Aditya", 13, "Abhishek", 4, 0, 13});
dt.Rows.Add(new object[] { "Abhishek", 4, "Saurabh", 6, 1, 16 });
dt.Rows.Add(new object[] { "Abhishek", 13, "Mohinder", 8, 1, 17 });
dt.Rows.Add(new object[] { "Mohinder", 8, "Mohammad", 14, 2, 18 });
dt.Rows.Add(new object[] { "Saurabh", 6, "Rahul", 1, 2, 11 });
dt.Rows.Add(new object[] { "Saurabh", 6, "Amitesh", 5, 2, 12 });
var result = BuildXML("", dt);
現在你有IEnumerable<XElement>
,把它變成字符串,你可以這樣做:
var xml = result.
Select(e => e.ToString()).
Aggregate((current, next) => current + next);
我想你可以利用以下代碼:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string connStr = @"Data Source=MY-PC\SQLExpress;Initial Catalog=DataDB;User Id=ME;Password=YourPassword;Trusted_Connection=True;";
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "Select MenuID, Text,Description, ParentID from UserInfo";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
da.Dispose();
}
ds.DataSetName = "UserInfos"; //You can start directly from here as you have the dataset just mantain Parent and Child ID Proper
ds.Tables[0].TableName = "UserInfo";
DataRelation relation = new DataRelation("ParentChild",
ds.Tables["UserInfo"].Columns["MenuID"],
ds.Tables["UserInfo"].Columns["ParentID"], true);
relation.Nested = true;
ds.Relations.Add(relation); //XmlDataSource1 is any source of xml you can have this in file also
XmlDataSource1.Data = ds.GetXml(); //Here Dataset will automatically generate XML for u based on relations added
}
您可以在數據集中創建DataRelation,例如
DataRelation relation = new DataRelation("ParentChild",
ds.Tables["UserInfo"].Columns["MenuID"],
ds.Tables["UserInfo"].Columns["ParentID"], true);
**relation.Nested = true;**
ds.Relations.Add(relation); //
歸屬“嵌套”非常重要!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.