-2

This is the result of an online xml :

<prices targetNamespace="http://api.saxo.com/v1/prices/">
    <price>
        <id>28924741-0-0-1-0-1-0</id>
        <quantity type="integer">1</quantity>
        <normalprice type="decimal">49,95</normalprice>
        <price type="decimal">49,95</price>
        <vatpercent type="decimal">25,00</vatpercent>
        <fixedprice>false</fixedprice>
        <discount type="decimal">0,00</discount>
        <allowdiscount type="integer">1</allowdiscount>
        <productid>28924741</productid>
        <entries>
            <entry>
                <id>1</id>
                <type type="PriceEntryType">Standard</type>
                <quantity type="integer">1</quantity>
                <vatpercent type="decimal">25,00</vatpercent>
                <vatamount type="decimal">9,99</vatamount>
                <priceunitexvat type="decimal">39,96</priceunitexvat>
                <priceunitinclvat type="decimal">49,95</priceunitinclvat>
                <pricetotalexvat type="decimal">39,96</pricetotalexvat>
                <pricetotalinclvat type="decimal">49,95</pricetotalinclvat>
                <discountpercent type="decimal">0,00</discountpercent>
                <discountamountexvat type="decimal">0,00</discountamountexvat>
                <discountamountinclvat type="decimal">0,00</discountamountinclvat>
            </entry>
            <entry>
                <id>2</id>
                <type type="PriceEntryType">Context</type>
                <quantity type="integer">1</quantity>
                <vatpercent type="decimal">25,00</vatpercent>
                <vatamount type="decimal">9,99</vatamount>
                <priceunitexvat type="decimal">39,96</priceunitexvat>
                <priceunitinclvat type="decimal">49,95</priceunitinclvat>
                <pricetotalexvat type="decimal">39,96</pricetotalexvat>
                <pricetotalinclvat type="decimal">49,95</pricetotalinclvat>
                <discountpercent type="decimal">0,00</discountpercent>
                <discountamountexvat type="decimal">0,00</discountamountexvat>
                <discountamountinclvat type="decimal">0,00</discountamountinclvat>
            </entry>
            <entry>
                <id>3</id>
                <type type="PriceEntryType">Subscription</type>
                <quantity type="integer">1</quantity>
                <vatpercent type="decimal">25,00</vatpercent>
                <vatamount type="decimal">6,99</vatamount>
                <priceunitexvat type="decimal">27,96</priceunitexvat>
                <priceunitinclvat type="decimal">34,95</priceunitinclvat>
                <pricetotalexvat type="decimal">27,96</pricetotalexvat>
                <pricetotalinclvat type="decimal">34,95</pricetotalinclvat>
                <discountpercent type="decimal">30,03</discountpercent>
                <discountamountexvat type="decimal">12,00</discountamountexvat>
                <discountamountinclvat type="decimal">15,00</discountamountinclvat>
            </entry>
        </entries>
    </price>
</prices>

I tried many ways to get value of "normalprice" and "pricetotalinclvat" of last entry . but i got null or exception .

Can you guide me that how i can get those two values by using linq ?

gmiley
  • 6,531
  • 1
  • 13
  • 25
peyman gilmour
  • 1,168
  • 2
  • 16
  • 35
  • 2
    Is there a need for Linq in this scenario? It would seem to be better to just use XDocument and access the desired value using an XPath statement. Also, if you have attempted something, please provide a sample of the code that is causing a problem. – gmiley Nov 09 '15 at 14:34
  • It would be better by linq , otherwise it does not matter ... How can i fix it with Xpath ? – peyman gilmour Nov 09 '15 at 14:39
  • 1
    Show what you have tried so far. – CSharpie Nov 09 '15 at 14:42

5 Answers5

1

Looks like a possible duplication of this
The short version is:

XElement element = XElement.Parse(YourString);
var prices = element.Elements("price")
          .Select(item => item.Element("normalprice").Value);
Community
  • 1
  • 1
YuriG
  • 368
  • 3
  • 14
1

These values can be extracted using Descendant in combination with Last:

  var xml = XElement.Parse(xmlStr);

  var normalPrice = xml
    .Descendants("normalprice")
    .Last()
    .Value;

  var pricetotalinclvat = xml
    .Descendants("pricetotalinclvat")
    .Last()
    .Value;
HashPsi
  • 1,391
  • 2
  • 12
  • 22
1

You can try this:

 XDocument doc = XDocument.Load(path);
 var query = from price in doc.Descendants("price")
             select new
                        {
                           NormalPrice = price.Element("normalprice").Value,
                           PriceTotalInclVat = price.Descendants("entry").Last().Element("pricetotalinclvat").Value
                        };

To avoid a null exception in case you don't have entries you can also do this:

var query = from price in doc.Descendants("price")
            select new
                      {
                         NormalPrice = price.Element("normalprice").Value,
                         PriceTotalInclVat = price.Descendants("entry").Any()?price.Descendants("entry").Last().Element("pricetotalinclvat").Value:"0"
                      };

Or:

var query = from price in doc.Descendants("price")
            let entries = price.Descendants("entry")
            select new
                       {
                         NormalPrice = price.Element("normalprice").Value,
                         PriceTotalInclVat = entries!=null ? entries.Last().Element("pricetotalinclvat").Value : "0"
                       };
ocuenca
  • 38,548
  • 11
  • 89
  • 102
1

If approach does not matter, loading the contents into an XDocument and accessing via XPath would seem to make more sense in this situation:

You will want to be using the System.Xml.XPath namespace with this...

    System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(xmlString);
    decimal priceNormal = 0;
    decimal.TryParse(xdoc.XPathSelectElement(@"/prices/price/normalprice").Value, out priceNormal);
    decimal priceTotalInclvat = 0;
    decimal.TryParse(xdoc.XPathSelectElement(@"/prices/price/entry[last()]/pricetotalinclvat").Value, out priceTotalInclvat);
gmiley
  • 6,531
  • 1
  • 13
  • 25
0

I tried all of the solutions in this page but i did not get any result . it is so weird .this is what i did , but it is not a good way :

  var document = XDocument.Load(url);
        var root = document.Root;

        if (root == null)
            return;

        var ns = root.GetDefaultNamespace();
        var mainNode = document.Element(ns + "prices");

        if (mainNode == null)
        return;


            var priceNode = mainNode.Elements(ns + "price").FirstOrDefault().Elements();
            var lastEntry = mainNode.Elements(ns + "price").FirstOrDefault().Elements().Last().Elements().Last().Elements();


            foreach (var element in lastEntry.Where(element => element.Name.LocalName == "pricetotalinclvat"))
            {
                plusPrice = element.Value;
            }

            foreach (var element in priceNode.Where(xElement => xElement.Name.LocalName == "price"))
            {
                price = element.Value;
            }

Any Suggestion to make it better ?

peyman gilmour
  • 1,168
  • 2
  • 16
  • 35