EDN Admin
Well-known member
Hi
Im completely new to Linq and Im trying to get an understanding if its the best approach to achieve my goal. Im trying to bind data to a ListBox, which is in Silverlight.
I am only ever going to read xml in this instance, and Im not sure if using XmlDocument or XDocument is the best (meaning most efficient and least memory intensive) approach, say Im using the following XML
<pre><?xml version="1.0" encoding="utf-8" ?>
<root>
<category id="1" name="Category 1
<subCategory id="1-1" name="Category 1-1
<product id="1-1-1" name="Product 1-1-1 </product>
<product id="1-1-2" name="Product 1-1-2 </product>
</subCategory>
<subCategory id="1-2" name="Category 1-2
<product id="1-2-1" name="Product 1-2-1 </product>
<product id="1-2-2" name="Product 1-2-2 </product>
</subCategory>
<subCategory id="1-3" name="Category 1-3
<product id="1-3-1" name="Product 1-3-1 </product>
<product id="1-3-2" name="Product 1-3-2 </product>
</subCategory>
</category>
<category id="2" name="Category 2
<subCategory id="2-1" name="Category 2-1
<product id="2-1-1" name="Product 2-1-1 </product>
</subCategory>
<subCategory id="2-2" name="Category 2-2
<product id="2-2-1" name="Product 2-2-1 </product>
<product id="2-2-2" name="Product 2-2-1 </product>
</subCategory>
</category>
</root>[/code]
What I will be trying to do is bind the products to a ListBox where I know the "category id" and I know the "subCategory id". Im already able to do this using this code:
<pre>XDocument data = XDocument.Load("my.xml");
<br/>var categories = (from category in data.Descendants("category")<br/> where category.Attribute("id").Value.Equals("1")<br/> select new<br/> {<br/> selected = category.Elements("subCategory")<br/><br/> }).ToList();<br/><br/>List<string> itemsList = new List<string>();<br/><br/>foreach (var subCategory in categories[0].selected)<br/>{<br/> if (subCategory.Attribute("id").Value == "1-2")<br/> {<br/> foreach (var product in subCategory.Elements("product"))<br/> {<br/> itemsList.Add(product.Attribute("name").Value);<br/> }<br/> }<br/>}
[/code]
<br/>
This approach works, but I am wondering if theres a better approach, or whats considered bast practice, that can use a Linq query to get straight to the products when I know the ids to get straight to the relevant node.
Thanks in advance!
View the full article
Im completely new to Linq and Im trying to get an understanding if its the best approach to achieve my goal. Im trying to bind data to a ListBox, which is in Silverlight.
I am only ever going to read xml in this instance, and Im not sure if using XmlDocument or XDocument is the best (meaning most efficient and least memory intensive) approach, say Im using the following XML
<pre><?xml version="1.0" encoding="utf-8" ?>
<root>
<category id="1" name="Category 1
<subCategory id="1-1" name="Category 1-1
<product id="1-1-1" name="Product 1-1-1 </product>
<product id="1-1-2" name="Product 1-1-2 </product>
</subCategory>
<subCategory id="1-2" name="Category 1-2
<product id="1-2-1" name="Product 1-2-1 </product>
<product id="1-2-2" name="Product 1-2-2 </product>
</subCategory>
<subCategory id="1-3" name="Category 1-3
<product id="1-3-1" name="Product 1-3-1 </product>
<product id="1-3-2" name="Product 1-3-2 </product>
</subCategory>
</category>
<category id="2" name="Category 2
<subCategory id="2-1" name="Category 2-1
<product id="2-1-1" name="Product 2-1-1 </product>
</subCategory>
<subCategory id="2-2" name="Category 2-2
<product id="2-2-1" name="Product 2-2-1 </product>
<product id="2-2-2" name="Product 2-2-1 </product>
</subCategory>
</category>
</root>[/code]
What I will be trying to do is bind the products to a ListBox where I know the "category id" and I know the "subCategory id". Im already able to do this using this code:
<pre>XDocument data = XDocument.Load("my.xml");
<br/>var categories = (from category in data.Descendants("category")<br/> where category.Attribute("id").Value.Equals("1")<br/> select new<br/> {<br/> selected = category.Elements("subCategory")<br/><br/> }).ToList();<br/><br/>List<string> itemsList = new List<string>();<br/><br/>foreach (var subCategory in categories[0].selected)<br/>{<br/> if (subCategory.Attribute("id").Value == "1-2")<br/> {<br/> foreach (var product in subCategory.Elements("product"))<br/> {<br/> itemsList.Add(product.Attribute("name").Value);<br/> }<br/> }<br/>}
[/code]
<br/>
This approach works, but I am wondering if theres a better approach, or whats considered bast practice, that can use a Linq query to get straight to the products when I know the ids to get straight to the relevant node.
Thanks in advance!
View the full article