Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.
C#
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")))));
//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();
Posted
Updated 9-Nov-12 21:45pm
v3
Comments
Andreas Gieriet 9-Nov-12 16:03pm    
What have you tried out so far?
Cheers
Andi
Member 9586006 9-Nov-12 22:58pm    
I've updated the question with the sample code that I have tried to test. If I use xmlds.DataFile=("<string-filepath>") instead of xmlds.DataFile=xdoc, the TreeView binding and the GridView binding works. But using xdoc directly does not work.
Member 9586006 10-Nov-12 8:00am    
Hi,
Says above that you have edited the code. Can you explain what was edited?
Andreas Gieriet 10-Nov-12 15:29pm    
From a certain reputation point level on, one is allowed to edit other posts. Usually, this is only fixing broken formatting. In your case, the code was not part of the pre-formatted C# section. You can click on v3 and see the differences.
Cheers
Andi
Member 9586006 10-Nov-12 20:35pm    
Thanks. I'm new to the forum, so I hope you'll excuse the goof-up. Hoping to find a solution to my problem. Been struggling with it for days.

1 solution

How about
C#
...
//create a datasource for TreeView or GridView using the xml document in memory.
XmlDataSource xmlds = new XmlDataSource();
xmlds.Data=xdoc.ToString();
xmlsd.ID="SomeGloballyUniqueNameForThisDataSource"; // e.g. "Products"
...


See also MSDN: XmlDataSource.Data property[^].

Cheers
Andi
 
Share this answer
 
v2
Comments
Member 9586006 12-Nov-12 7:54am    
Yeah, that works, if we insert after the first line
xmlds.ID="xmldsID";
Thanks. Can you edit your answer to Accept the solution?
Andreas Gieriet 12-Nov-12 8:01am    
My understanding: This ID has nothing to do with the data, this has to do with how you access it in the GUI, i.e. that ID "xmldsID" is your choice.
Cheers
Andi
Member 9586006 12-Nov-12 8:22am    
Tried your solution, but it gave the exception error:"When caching is enabled for the XmlDataSource that is not in the page's control tree it requires a UniqueID that is unique throughout the application." So I tried giving an ID and it worked. Is there any other way of avoiding the exception error?
Andreas Gieriet 12-Nov-12 11:13am    
I don't know. I think you did it right. The data itself does not need that, but the client of the data (the GUI) seems to need a unique ID. It's under your control to give a decent id.
Cheers
Andi
PS: I will adapt the solution so that it works as you explained. Thanks for your hint.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900