I am trying to find a way to use the result from my linq query as a datasource for a TreeView and GridView in C#, without having to save the file first in the server. Have tried various options, but all require that the document is saved as a file first. Can someone please help me out? The code is given below:
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", "Rs.")),
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", "Rs.")),
new XElement("stock", "25"),
new XElement("country", "USA",
new XElement("state", "Florida")))));
//create a linq query
var newxml = from f1 in products.Elements("product")
where (string)f1.Element("country").Element("state") != "Florida"
select f1;
//Create an xml document in memory using the linq query
XDocument xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XElement("products"));
xdoc.Element("products").Add(newxml);
//create a datasource for TreeView or GridView using the xml document in memory.
XmlDataSource xmlds = new XmlDataSource();
xmlds.DataFile=xdoc;
TreeView1.DataSource = xmlds;
TreeView1.DataBind();
GridView1.DataSource = xmlds;
GridView1.DataBind();
The part of the code for creating the datasource from xdoc is not working. It can be made to work by saving the file and then calling the file for the datasource, but I want do do this from the memory.