none
Sometimes a parent node doesn't have a child node xml xpath RRS feed

  • Question

  • Hi,

    I am reading an xml file using XDocument and XPath, in my query I am reading like so:

    var elements = doc.XPathSelectElements("something/somethingelse"); var items = from e in elements select new { name = e.Attribute("name")?.Value, age = e.Attrubute("age")?.Value, details = from d in e.XPathSelectElements("info") select new { status = d.Attribute("statusReport")?.Value, number = d.XPathSelectElement("shirt").Attribute("number")?.Value, } }

    After I foreach loop through the 'items' and 'details' collections.


    if the statusReport = "inActive" there will not be a xpathselectElement("shirt") or a .Attribute("number") and I get the object not set to an instance of an object exception. How can I prevent this happening?

    Thank you for your help.  


    CuriousCoder



    Saturday, May 25, 2019 4:16 PM

All replies

  • Hi CuriousCoder15,

    Thank you for posting here.

    For your question, could you provide the related xml?

    We will solve your problem in time if we get the xml.

    We are waiting for your update.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 27, 2019 6:41 AM
    Moderator
  • Hi Jack,

    I have amended my code after reading your post here:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/f133f2e9-b269-4f8b-816a-861eeff4b24c/getting-the-last-xpathselectelementattributevalue-c?forum=csharpgeneral#bf7e235b-2d3e-4c5f-b9a0-b35f32767953

    and it's probably easier to use the xml in that post as an example so here it is again:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE Racing SYSTEM "Racing.dtd">
    <Racing timestamp="20190322T150832+0100">
    <Meeting id="109" status="" date="20190322" Destination="UK">
    <Event id="88" status="" date="20190322" noContestants="5" time="1510+0100">
    <Contestant id="22" status="" name="Troy Mc Clean">
    <Shirt number="1"/>
    <Player id="11" name="Morgan Spice">
    </Player>
    <Coach id="13" name="John Smith"/>
    <Game timestamp="20190322T150333+0100" EventNumber="1">
    <Odds denom="8" numer="15"/>
    </Game>
    <Game timestamp="20190322T150539+0100" EventNumber="1">
    <Odds denom="4" numer="7"/>
    </Game>
    <Game timestamp="20190322T150546+0100" EventNumber="1">
    <Odds denom="8" numer="15"/>
    </Game>
    <Game timestamp="20190322T150703+0100" EventNumber="1">
    <Odds denom="1" numer="7"/>
    </Game>
    </Contestant>
    <Contestant></Contestant>
    <Contestant></Contestant>
    <Contestant></Contestant>
    <Contestant></Contestant>
    </Event>
    <Event></Event>
    <Event></Event>
    <Event></Event>
    </Meeting>
    </Racing>


    If the Descendant("Odds") is missing then when I loop through it throws an object instance exception. 

    As per your suggested code when I now read the xml like so:

    denom = c.Descendants("Odds").Last().Attribute("denom")?.Value,
    timestamp = c.Descendants("Game").Last().Attribute("timestamp")?.Value,
    numer = c.Descendants("Odds").Last().Attribute("numer")?.Value
     

    On some occasions I noticed that "Odds" node isn't in the file which means no denom or numer can be read.

    I have created a bool of oddsExist = doc.Desendants("Odds").Any(); which helps ignore files that don't have them at all but what do I do if the file does have them and it is missing in a part of the file?

    Many Thanks 


    CuriousCoder

    Monday, May 27, 2019 9:38 AM
  • Hi 

    Thanks for the feedback.

    I don't quite understand what you mean, if there is no "odds" node in the file, then why are we going to read it? If there are only a few odd nodes in the file, I tried the code and it doesn't show an exception.

    Could you give an example of your problem? It is best for you to give the xml that will occur the exception.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 28, 2019 7:18 AM
    Moderator