[RESOLVED]Linq to XML help

My XML structure looks like this.

If I want to find out the value for Manufacturer which is Lenovo

how would i write the linq to xml?

i’ve got this now and it doesn’t work

XElement root = XElement.Load(file);
IEnumerable<XElement> address =
from el in root.Elements("item")
//where (string)el.Attribute("Property") == "Manufacturer"
select el;
foreach (XElement el in address)
{
MessageBox.Show(el.Value);
}

<item Property="Computer" >
<item Property="Manufacturer" Value="LENOVO" />
<item Property="Family" Value="Not Available" />
<item Property="Product Name" Value="10A9000TAU" />
<item Property="Model" Value="10A9000TAU" />
<item Property="Version" Value="ThinkCentre M93p" />
<item Property="Serial Number" Value="PB000400" />
<item Property="SMBIOS Asset Tag" Value="Not Available" />
<item Property="SKU Number" Value="Lenovo" />
<item Property="Chassis" Value="Not Available" />
<item Property="PC System Type" Value="Desktop" />
<item Property="Machine Type" Value="AT/AT COMPATIBLE" />
<item Property="Firmware Type" Value="BIOS" />
<item Property="Infrared (IR) Supported" Value="No" />
<item Property="UUID" Value="123456789" />
</item>

Hi

Following will return the "Lenovo". Try 

var items = (from i in doc.Descendants("item").Descendants("item")
                             where i.Attribute("Property").Value == "Manufacturer"
                             select i.Attribute("Value").Value
                             );

That’s great.

Due to the fact there’s several nodes with Manufacturer.

How do i filter to only Computer then Manufacturer?

From Chandrasekar’s answer, try to add an AND operator for Computer like:

var items = (from i in doc.Descendants("item").Descendants("item")
                             where i.Attribute("Property").Value == "Manufacturer" && i.Attribute("Property").Value == "Computer"
                             select i.Attribute("Value").Value
                             );

based on that code, it doesn’t pick up any data.

because Computer is a node before manufacturer.

They are on different descendants.

Not sure if code below will work but the idea is to get the descendants of Computer first. Then from the results, you get the Manufacturer.

var items1 = (from i in doc.Descendants("item")
              where i.Attribute("Property").Value == "Computer"
             );

var items2 = (from i in items1.Descendants("item")
              where i.Attribute("Property").Value == "Manufacturer"
              select i.Attribute("Value").Value
             );

 

Hi

Try the following. If you could post the entire xml data or some portion of the xml which has another items other than "Computer" it would be easy for us to answer the post.

var items = (from i in doc.Descendants("item").Descendants("item")
             where i.Attribute("Property").Value == "Manufacturer" && i.Parent.Attribute("Property").Value == "Computer"
                             select i.Attribute("Value").Value
                             );

Leave a Reply