Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

How to find the right Element

$
0
0

Im new with Linq to XML (but have used Linq to SQL). After some research i have created some code for the below XML 

<?xml version="1.0"?><terms xmlns:xsi="http://www.w3.org/" xmlns:xsd="http://www.w3.org/" name="Hotels"><hotels><hotel name="Hotel 1"><rooms><room reference="123"><availableFrom /><department>External</department><roomofweek>Yes</roomofweek><price_text>50</price_text><priority>Top</priority><room_number>01</room_number><brochure /><room_type>Standard</room_type><rooms /><pictures><picture name="Interior"><filename>1.jpg</filename></picture><picture name="Bathroom"><filename>3.jpg</filename></picture><picture name="Hall way"><filename>2.jpg</filename></picture><picture name="Balcony"><filename>5.JPG</filename></picture></pictures></room><room reference="a123"><availableFrom /><department>External</department><roomofweek>Yes</roomofweek><price_text>100</price_text><priority>Top</priority><room_number>1000</room_number><brochure /><room_type>Executive</room_type><rooms /><pictures><picture name="Interior"><filename>1.jpg</filename></picture><picture name="Bathroom"><filename>3.jpg</filename></picture><picture name="Hall way"><filename>2.jpg</filename></picture><picture name="Balcony"><filename>5.JPG</filename></picture></pictures></room></rooms></hotel><hotel name=" Hotel 2"><rooms><room reference="B3456"><availableFrom /><department>Internal</department><roomofweek>No</roomofweek><price_text>40</price_text><priority>Med</priority><room_number>02</room_number><brochure /><room_type>Economy</room_type><rooms /><pictures><picture name="Interior"><filename>1.jpg</filename></picture><picture name="Bathroom"><filename>3.jpg</filename></picture><picture name="Hall way"><filename>2.jpg</filename></picture><picture name="Balcony"><filename>5.JPG</filename></picture></pictures></room></rooms></hotel></hotels></terms>

To read/load the file

XDocument Xdoc = XDocument.Load(XMLFilePath);
Now i would like to get all the price_text from all individual room:

foreach (var i in Xdoc.Descendants("hotels"))
{
  if (i.Element("price_text").Value == "price_text")

    {
        // Do some data operations
    }
}

But i always get "Additional information: Object reference not set to an instance of an object."?

The idea is to get all the hotels and each individual hotel which has many rooms (which could be 100's) and perform some relevant tasks. How could i resolve this issue?


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>