|
|
||||||
|
#1
|
|
|
|
|
Maybe I shouldn't be using XML for what I want. I want to store some
program data and retrieve it easily, iterating through elements grabbing child elements easily and reading attributes along the way. Let's say I want to use the following statement (and yes it works in C#): XElement contactsParsedIn = XElement.Parse( @"<contacts> <contact> <name>Patrick Hines</name> <items> <item number='1'>onep</item> </items> </contact> <contact> <name>Gretchen Rivas</name> </contact> <contact> <name>Scott MacDonald</name> <phone type='home'>925-555-0134</phone> <phone type='mobile'>425-555-0177</phone> <section sn='1'> <items> <item number='1'>1onem</item> <item number='2'>1twom</item> <item number='3'>1threem</item> <item number='4'>1fourm</item> <item number='5'>1fivem</item> </items> </section> <section sn='2'> <items> <item number='1'>2onem</item> <item number='222'>2twom</item> <item number='333'>2threem</item> <item number='444'>2fourm</item> <item number='5'>2fivem</item> </items> </section> </contact> <contact> <name>Patrick Smith</name> </contact> </contacts>"); And let's further say that I want to check out the info about Scott MacDonald where I would like to find the items his "section" element where the sn attribute = 2 and then I want to iterate through the items in that section and get the number attribute for each item and the value of the associated element. So far I have seen no Linq to XML samples that handle this type of situation. Certainly not in an elegant way. Pseudo code for what I want might be like: Get contact element where name =="scot McDonald" Move to the section elements until sn attribute == 2 Foreach( item in items) { writeline("number attribute = " + item.attribute.number; writeline("item value=" + item.element.value; } And the output would look like this: number attribute=1 item value=2onem number attribute=222 item value=2twom number attribute=333 item value=2threem number attribute=444 item value=2fourm number attribute=5 item value=2fivem I would appreciate any useful hints or ideas. Or if you can bang out the general code or close to it, I would love to take that and run with it. Thank you, Reece |
|
|
|
#2
|
|
|
|
|
Reece wrote:
[..] > Pseudo code for what I want might be like: > > Get contact element where name =="scot McDonald" > Move to the section elements until sn attribute == 2 > Foreach( item in items) > { > writeline("number attribute = " + item.attribute.number; > writeline("item value=" + item.element.value; > } > Constructing "items" could look like this in LINQ to XML: var items = from contact in contactsParsedIn.Elements("contact") where contact.Element("name").Value == "Scott MacDonald" let section = contact.Elements("section").Where(s => (int) s.Attribute("sn") == 2).First() from item in section.Element("items").Elements("item") select new { Number = (int) item.Attribute("number"), Value = item.Value }; I find this pretty straightforward, though it will be more elegant once LINQ to XSD arrives and allows strong typing; in VB.NET, this can already be written down more elegantly. If you don't like LINQ to XML, XPath still works. Look at XElement.XPathSelectElements(). I have no time to construct the corresponding expression now, as I have to leave for work. :-) |
|
#3
|
|
|
|
|
Thank you, Jeroen. It compiled and now I will study it and digest it.
Thanks again, Reece "Jeroen Mostert" <jmostert> wrote in message news:514c [..] |
|
#4
|
|
|
|
|
Reece wrote:
> Thank you, Jeroen. It compiled and now I will study it and digest it. > You may find these resources useful for your study: http://msdn.microsoft.com/vcsharp/aa336746 (101 LINQ samples) http://msdn.microsoft.com/library/bb387098 (the main LINQ to XML documentation) |
|
#5
|
|
|
|
|
I now believe in Linq to XML. I had looked at the main page you link to and
several other web pages before my original post, but you brought it all together for me with your query and the link to the 101 LINQ samples which allowed me to put together the following statement that works with your query: foreach (var myitem in items) Console.WriteLine(myitem.Number + " " + myitem.Value); You helped me cross the bridge. I just needed to see it all working together. Big thanks again! Reece. "Jeroen Mostert" <jmostert> wrote in message news:514c [..] |
|
|
| Similar Threads | |
| Full compliance with Code Analysis still possible when using LINQ? I would like to be in full-compliance with Code Analysis. However, since I am using LINQ to SQL, I am frequently re-generating my DataClasses.dbml as the data model... |
|
| Injecting code into linq Suppose I have the following class which maps to a table: [Table] public class People { [Column( IsDbGenerated = true, IsPrimaryKey = true )] public int PersonId { get; set;... |
|
| Why northwind linq code has unused methods I looked into northwind database sample code in MS Linq samples Every entity has 3 partial methods: #region Extensibility Method Definitions partial void... |
|
| Linq - Relation in .dbml but not in code behind Hi I have a db the looks like DVD - (table) id (primary key , identity) title categoryId (fk on Category Id) ..... |
|
| LINQ code generation? I'm not clear on this - does LINQ just use a code generator to give the developer classes that represent stored procedures? If so, won't that in effect just create the same... |
|
|
All times are GMT. The time now is 09:53 PM. | Privacy Policy
|