views:

32

answers:

2

Hi,

I have an XML string that looks like this:

<Attributes>
    <ProductAttribute id="1">
        <ProductAttributeValue>
            <Value>a</Value>
        </ProductAttributeValue>
    </ProductAttribute>
    <ProductAttribute id="2">
        <ProductAttributeValue>
            <Value>a</Value>
        </ProductAttributeValue>
        <ProductAttributeValue>
            <Value>b</Value>
        </ProductAttributeValue>
    </ProductAttribute>    
</Attributes>

I would like to return an IEnumerable like this:

Id Value
1  a
2  a b

I have tried this and only got the "b" value for Id "2":

XElement e = XElement.Parse(xmlString);
var q = from pa in e.Elements("ProductAttribute")
from pav in pa.Elements("ProductAttributeValue").Elements("Value")
select new
{
Id = (int)pa.Attribute("id"),
Value = (string)pav
};

I tried this:

 XElement e = XElement.Parse(xmlString);
    var q = from pa in e.Elements("ProductAttribute")
    select new
    {
    Id = (int)pa.Attribute("id"),
    Value = pa.Elements("ProductAttributeValue").Elements("Value")
    };

But could not cast Value as a string. Using LINQPad the output was like this:

Id Value
1  a
2  <Value>a</Value>
   <Value>b</Value>

I am trying to just return the values. Is this even possible?

Thanks.

A: 

If you wanted a contatenated string of those values like "a b"

 XElement e = XElement.Parse(xmlString);
    var q = from pa in e.Elements("ProductAttribute")
    select new
    {
    Id = (int)pa.Attribute("id"),
     Value = string.Join(" " ,
                    pa.Elements("ProductAttributeValue")
                     .Elements("Value")                                            
                     .Select(x=>x.Value)
                     .ToArray())
    };
p.campbell
This is also very helpful! Thanks campbell. FYI there is a closing parenthesis missing from your code example. Other than that it worked perfectly.
@joebloe: thanks for the note on the paren. Remember to vote-up (when you have enough rep) and choose an 'accepted answer' as part of the reputation system on StackOverflow. Give reputation to those who take the time to answer questions. I voted your question, as I thought it was a good one, and to help get you some rep.
p.campbell
+1  A: 
XElement e = XElement.Parse(xmlString); 
var q = from pa in e.Elements("ProductAttribute") 
select new 
{ 
Id = (int)pa.Attribute("id"), 
Value = from pav in pa.Elements("ProductAttributeValue").Elements("Value") select pav.Value 
}; 

Of course, Value will be an IEnumerable<string>.

Edit:

If you want the output to concat the Value elements into one string you can do this:

XElement e = XElement.Parse(xmlString); 
var q = from pa in e.Elements("ProductAttribute") 
select new 
{ 
Id = (int)pa.Attribute("id"), 
Value = string.Join(" ", (from pav in pa.Elements("ProductAttributeValue").Elements("Value")
        select pav.Value).ToArray())
};

Then the output will be:

Id Value
1  a 
2  a b
Richard Hein
That's it! I tried a few variations with 'in' but couldn't get the syntax right. Thanks Richard.
Your welcome ...
Richard Hein