none
Can't get InnerText of a node in an XML document RRS feed

  • Question

  • This is a really simple problem and I'm having trouble solving it.

    I have an XML document. It's structure is like this:

    <Scenario>

        <ScenarioDescription>

        <Other stuff...   >

    I need to get the InnerText of the ScenarioDescription. It only appears once in the XML file. There is only one node called <ScenarioDescription>

    Here's my C# code:

                    xml.Load(s);
                    XmlNode root = xml.DocumentElement;
                    XmlNode test = root.SelectSingleNode("//Scenario/*");
                    ScenarioText = test.InnerText;

    test is returning NULL,

    Help!


    Saturday, December 28, 2019 9:58 PM

Answers

  • I think that even if you write root.SelectSingleNode("//Scenario/ScenarioDescription"), it will not work when XML contains namespaces (declared with xmlns attributes).

    Show more details about the real XML that reproduce the problem.

    • Marked as answer by Zetar Tuesday, January 7, 2020 12:54 PM
    Saturday, December 28, 2019 10:34 PM
  • Here's the solution:

    temp = Path.GetFileNameWithoutExtension(s);
    var document = new XmlDocument();
    document.Load(s);
    
    var root = document.DocumentElement;
    var node = root["ScenarioDescription"];
    var text = node?.InnerText;

    Sunday, December 29, 2019 7:07 PM

All replies

  • I think that even if you write root.SelectSingleNode("//Scenario/ScenarioDescription"), it will not work when XML contains namespaces (declared with xmlns attributes).

    Show more details about the real XML that reproduce the problem.

    • Marked as answer by Zetar Tuesday, January 7, 2020 12:54 PM
    Saturday, December 28, 2019 10:34 PM
  • You are absolutely correct. I remembered that this XML had been serialized. Here's the header:

    <?xml version="1.0"?>
    
    -<Scenario xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ModelLib">
    
    
    -<ArmyFiles xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    
    <a:string/>
    
    <a:string>The Confederate Army July 21, 1861</a:string>
    
    <a:string>Army of Northern Virginia</a:string>
    
    </ArmyFiles>
    
    
    -<AttackFactors xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">

    How do I read this?

    Saturday, December 28, 2019 11:32 PM
  • Here's the solution:

    temp = Path.GetFileNameWithoutExtension(s);
    var document = new XmlDocument();
    document.Load(s);
    
    var root = document.DocumentElement;
    var node = root["ScenarioDescription"];
    var text = node?.InnerText;

    Sunday, December 29, 2019 7:07 PM
  • Hi Zetar,

    It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    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, January 7, 2020 2:47 AM