Q:

Using a where condition within an inner element in XML

I am trying to parse an XML document and store the data in an array using LINQ in C# where I have multiple inner element with different data their attributes with the same name and it looks like 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Catalog>
 <Book ISBN="1.1.1.1" Genre="Thriller">
  <Title  PublishDt="2015-07-09">
   <Pty R="1" ID="Buyer_Company">
   </Pty>
   <Pty R="2" ID="Seller_Company">
   </Pty>
    </Title>
</Book>
</Catalog>

I am trying to store all the above data in an array I am not sure how to deal with the element Pty where I have to store Buyer_Company in Buy and Seller_Company in Sell as they have same Element name and Attribute name ID.
I got the remaining data as follows
 var result = doc.Descendants("Book")
        .Select(b => new
        {
            ISBN= b.Attribute("ISBN").Value,
            Genre=b.Attribute("Genre").Value,

            PublishDate= b.Element("Title").Attribute("MMY").Value,        
           Sell=b.Element("Title").Element("Pty").Attribute("ID").Value.Where......
Buy=b.Element("Title").Element("Pty").Attribute("ID").Value.Where......(this is where I have issues)
        })
        .ToArray();

May I know how I can use WHERE condition in inner element and attributes of XML in LINQ in C# such R=1 represents Buyer and R=2 represents Seller?

A:

You need to think in sequences.  You want to query over all your Pty elements, then filter them by the R attribute value, then get the property you want.
So your buyer id could be obtained by:
b.Descendants("Pty")
    .Where(e => (int)e.Attribute("R") == 1)
    .Select(e => (string)e.Attribute("ID"))
    .Single();

And a similar query for your seller id (change 1 to 2).  Putting it all together, you might end up with something like this.  I moved the cast to the start of the expression to make it clearer what type your properties will be.
var result = from book in doc.Descendants("Book")
             select new
             {
                 ISBN = (string)book.Attribute("ISBN"),
                 Genre = (string)book.Attribute("Genre"),
                 PublishDate = (DateTime)book.Elements("Title")
                     .Select(e => e.Attribute("PublishDt"))
                     .Single(),
                 Buyer = (string)book.Descendants("Pty")
                     .Where(e => (int)e.Attribute("R") == 1)
                     .Select(e => e.Attribute("ID"))
                     .Single(),
                 Seller = (string)book.Descendants("Pty")
                     .Where(e => (int)e.Attribute("R") == 2)
                     .Select(e => e.Attribute("ID"))
                     .Single()
             };

