Linq to xml ile bir xml dosyası oluşturup çalışma zamanı da Treeview kontrolünde gösterelim.
//aspx
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="Categories" Text="Categories" />
<asp:TreeNodeBinding DataMember="Category" TextField="CategoryName" ValueField="CategoryID" />
</DataBindings>
</asp:TreeView>
</div>
</form>
</body>
//cs
private void bindTree()
{
northwindDataContext db = new northwindDataContext();
string path = Server.MapPath(".\\App_Data\\data.xml");
if (!File.Exists(path))
{
XDocument xdoc = new XDocument(
new XDeclaration("1.0", "utf-8","yes"),
new XElement("Categories",
from p in db.Categories
orderby p.CategoryName
select
new XElement("Category",
new XAttribute("CategoryID", p.CategoryID),
new XAttribute("CategoryName", p.CategoryName))));
xdoc.Save(path);
}
XmlDataSource xd = new XmlDataSource();
xd.ID = "xid1";
xd.DataFile = path;
this.form1.Controls.Add(xd);
TreeView1.DataSourceID = "xid1";
}