I am sure there is an easy answer, but I'm not able to find it anywhere. I have a linq query on an xml file, the result of which is a filtered xml file. I can save this result as an xml file in the server and then call the file as a datasource for my TreeView control. But is there any way of using the query directly from the memory as a datasource for my TreeView control. Please help.
XDocument products = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XElement("products",
new XElement("product", new XAttribute("id", "p1"),
new XElement("name", "Alpha"),
new XElement("Address", "200 WATERLOO DRIVE"),
new XElement("price",
new XElement("currency", "USD")),
new XElement("stock", "19"),
new XElement("country", "USA",
new XElement("state", "California"))),
new XElement("product", new XAttribute("id", "p2"),
new XElement("name", "Beta"),
new XElement("Address", "500 MOUNTBATTEN AVENUE"),
new XElement("price",
new XElement("currency", "USD")),
new XElement("stock", "25"),
new XElement("country", "USA",
new XElement("state", "Florida")))));
var newxml = from f1 in products.Elements("product")
where (string)f1.Element("country").Element("state") != "Florida"
select f1;
XDocument xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XElement("products"));
xdoc.Element("products").Add(newxml);
XmlDataSource xmlds = new XmlDataSource();
xmlds.DataFile=xdoc;
TreeView1.DataSource = xmlds;
TreeView1.DataBind();
GridView1.DataSource = xmlds;
GridView1.DataBind();