How can I parse xml using linq in sort order
Last post 05-12-2008 11:22 AM by sladapter. 5 replies.
Sort Posts:
05-09-2008 3:37 AM
How can I parse xml using linq in sort order

Hi All,

I have a xml like this

<LowestNewPrice>
  <Amount>1519</Amount>
  <CurrencyCode>USD</CurrencyCode>
  <FormattedPrice>$15.19</FormattedPrice>
</LowestNewPrice>
<LowestNewPrice>
  <Amount>1519</Amount>
  <CurrencyCode>USD</CurrencyCode>
  <FormattedPrice>$9.09</FormattedPrice>
</LowestNewPrice>
<LowestNewPrice>
  <Amount>1519</Amount>
  <CurrencyCode>USD</CurrencyCode>
  <FormattedPrice>$21.14</FormattedPrice>
</LowestNewPrice>
<LowestNewPrice>
  <Amount>1519</Amount>
  <CurrencyCode>USD</CurrencyCode>
  <FormattedPrice>$5.00</FormattedPrice>
</LowestNewPrice>
<LowestNewPrice>
  <Amount>1519</Amount>
  <CurrencyCode>USD</CurrencyCode>
  <FormattedPrice>$17.13</FormattedPrice>
</LowestNewPrice>

I have written linq query for it is:

IEnumerable AmazonParseContent = from ItemAttributes in xdocument.Descendants("Item")

select new ResultedData

{

 

Price = (string)(from Item in ItemAttributes.Descendants("LowestNewPrice")

where Item != null

select Item.Element("FormattedPrice")

).ToList().FirstOrDefault(),

};

And I want this price list in sorted order means the price should be in list minmum to maximum.

Please answer

Thanks

vijchn

Joined on 04-29-2008
Posts 16
05-09-2008 10:35 AM
Re: How can I parse xml using linq in sort order

var AmazonParseContent = from ItemAttributes in xdocument.Descendants("Item")

select new ResultedData

{

Price = (string)(from Item in ItemAttributes.Descendants("LowestNewPrice")

where Item != null

select Item.Element("FormattedPrice")

).ToList().FirstOrDefault(),

};

List<ResultedData> list = AmazonParseContent.OrderBy(r => r.Price).ToList();

 

Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Joined on 03-05-2008
Indiana, US
Posts 404
05-12-2008 1:13 AM
Re: How can I parse xml using linq in sort order

List<ResultedData> list = AmazonParseContent.OrderBy(r => r.Price).ToList();

In this line I didn't understand what is the means of r here.

The second thing is that AmazonParseContent doesn't contains the definition of OrderBy

so plese suggest me some another way.

Thanks

vijchn

Joined on 04-29-2008
Posts 16
05-12-2008 2:13 AM
Marked as Answer
Re: How can I parse xml using linq in sort order

Hi:

  Say the xml is like this:

<Root>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$15.19</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$9.09</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$21.14</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$5.00</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$17.13</FormattedPrice>
  </LowestNewPrice>
</Root>

  You can try:

   XDocument xmlProducts = XDocument.Load("uri_of_xml_file");
            var products = from ItemAttributes in xmlProducts.Descendants("LowestNewPrice")

                           select new

                           {

                               Price = ItemAttributes.Element("FormattedPrice").Value

                           }
                           ;

var result =products.OrderBy(r =>Convert.ToDouble(r.Price.Substring(1,r.Price.Length-1))).ToList();

  I suggest you to learn two concepts: lambda expression and extension methods, which are very important in Linq.

http://msdn.microsoft.com/en-us/library/bb397687.aspx

http://msdn.microsoft.com/en-us/library/bb675269.aspx

Regards

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Allen Chen – MSFT

Joined on 03-16-2007
Posts 717
05-12-2008 8:16 AM
Re: How can I parse xml using linq in sort order

But What I can do if price value is null in the xml in some tag. Like this

<Root>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$15.19</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$9.09</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$21.14</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
    <FormattedPrice>$5.00</FormattedPrice>
  </LowestNewPrice>
  <LowestNewPrice>
    <Amount>1519</Amount>
    <CurrencyCode>USD</CurrencyCode>
  </LowestNewPrice>
</Root>

It gives Null reference exception in it.

var result =products.OrderBy(r =>Convert.ToDouble(r.Price.Substring(1,r.Price.Length-1))).ToList();

So please give me suggestion for it.

Thanks with Regards

 

vijchn

Joined on 04-29-2008
Posts 16
05-12-2008 11:22 AM
Marked as Answer
Re: How can I parse xml using linq in sort order

Try this:

 string s = @"                          
                        <Root>
                      <LowestNewPrice>
                        <Amount>1519</Amount>
                        <CurrencyCode>USD</CurrencyCode>
                        <FormattedPrice>$15.19</FormattedPrice>
                      </LowestNewPrice>
                      <LowestNewPrice>
                        <Amount>1519</Amount>
                        <CurrencyCode>USD</CurrencyCode>
                        <FormattedPrice>$9.09</FormattedPrice>
                      </LowestNewPrice>
                      <LowestNewPrice>
                        <Amount>1519</Amount>
                        <CurrencyCode>USD</CurrencyCode>
                        <FormattedPrice>$21.14</FormattedPrice>
                      </LowestNewPrice>
                      <LowestNewPrice>
                        <Amount>1519</Amount>
                        <CurrencyCode>USD</CurrencyCode>
                        <FormattedPrice>$5.00</FormattedPrice>
                      </LowestNewPrice>
                      <LowestNewPrice>
                        <Amount>1519</Amount>
                        <CurrencyCode>USD</CurrencyCode>
                      </LowestNewPrice>
                    </Root>";

            XDocument xd = XDocument.Parse(s);
            var AmazonParseContent = from item in xd.Descendants("LowestNewPrice")
                                     where item.Element("FormattedPrice") != null
                                     orderby System.Convert.ToDouble(item.Element("FormattedPrice").Value.ToString().Substring(1))
                                     select new ResultData
                                     {
                                         Amount = (int)item.Element("Amount"),
                                         CurrencyCode = (string)item.Element("CurrencyCode"),
                                         Price = (string)item.Element("FormattedPrice")
                                     };

            List<ResultData> List = AmazonParseContent.ToList();
          

Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Joined on 03-05-2008
Indiana, US
Posts 404