0

Firstly, I am sorry - I have searched and searched, tried example after example, answer after answer and after trying lots of different examples, I still cannot get this to work for myself.

I have an XML smoething like this:

<?xml version="1.0" encoding="utf-8"?>
<Backup LastRun="Saturday, May 16, 2020 7:58:53 PM">
    <MyVehicleElements>
        <InVehicle>true</InVehicle>
        ... etc ...
    </MyVehicleElements>
</Backup>

And I my code, fails returning a null exception as I cannot for the life of me work out how to read the extra level.

XElement xelement = XElement.Load("PathTo\\File.xml");
IEnumerable<XElement> Backup = xelement.Elements();
     foreach (var MyVehicleElements in Backup)
     {
         Game.DisplayNotification(MyVehicleElements.Element("InVehicle").Value); 
         // Should return text "true"
     }

Please can somebody help me before my head explodes. Thank you in advance.

  • I can't reproduce any problem, see https://dotnetfiddle.net/XlDVXI. The value printed for `MyVehicleElements.Element("InVehicle").Value` is `true`. We'll need to see a [mcve] (a compilable, standalone example console app showing the problem without any references to external objects like `Game`) to help you. For general help see [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/3744182). – dbc May 16 '20 at 21:57
  • which line in your the code throws 'NullReferenceException'? is it `xelement.Elements()` or `MyVehicleElements.Element("InVehicle")`? – akg179 May 16 '20 at 22:21
  • You may have an encoding issue. When you post xml it may of changed the encoding on the characters. Try using posted xml and see if you still have an error. – jdweng May 16 '20 at 23:40
  • The code written by you is good, so please share the whole xml it might have encoding issue. – Laxmi Lal Menaria May 17 '20 at 06:33
  • Thank you all. My full XML is here: https://pastebin.com/e01tTsKU And my example code here: https://dotnetfiddle.net/C1Z5rZ although this is throwing an exception "System.IO.DirectoryNotFoundException" – Waynieoaks May 17 '20 at 09:19
  • 1
    You have to issues 1) Do not use Value with Xml Linq. When the object doesn't exist you will get an exception. Instead cast like this : var InVehicle = (string)GetElements.Element("InVehicle"); 2) InVehicle is not a child it is a descendant so use this : IEnumerable MyVehicleElements = xelement.Descendants("MyVehicleElements"); – jdweng May 17 '20 at 09:31
  • Hi - Thank you - it is working now... I knew it would have been something simple I was just getting stuck running in circles... – Waynieoaks May 17 '20 at 09:56

1 Answers1

0

Thank you for everyone above that helped. Working code for anyone else coming across this is:

      XElement xelement = XElement.Load("PathTo\\File.xml");
      IEnumerable<XElement> MyVehicleElements = xelement.Descendants("MyVehicleElements");
      foreach (var GetElements in MyVehicleElements)
      {
         Game.DisplayNotification((string)GetElements.Element("InVehicle"));
      }