简体   繁体   中英

c# how to use xml.SelectSingleNode method

I'm using c# .NET 4.8 and I'm trying to extract a value from an XML document. Here is a sample of the document I'm using:

<SOME_GROUP SomeValue="2">    
    <SOME_PLACEHOLDER SOMEDateTime="2022-02-10T14:50:48-08">
        <SOME_DATA>
            <MAIN_SOME SomeValue="2" MAINSOMEID="MAINSOME1">                
                <TARGET_FIELD MAINFIELDID="MAIN111" SomeID="somedata" _Value="someValue">
                    <_SUB_FIELD _Code="038" _Text="some other data"></_SUB_FIELD>                    
                </TARGET_FIELD>                
            </MAIN_SOME>
        </SOME_DATA>
    </SOME_PLACEHOLDER>
</SOME_GROUP>

I want to get the _Value from TARGET_FIELD and store it in a variable. Right now I can do this using the method SelectNodes() like this:

using System.Xml;

XmlDocument someXmlDocument = new XmlDocument();
someXmlDocument.LoadXml(someXmlFile);

var myValue = someXmlDocument.SelectNodes("/SOME_GROUP/SOME_PLACEHOLDER/SOME_DATA/MAIN_SOME/TARGET_FIELD")
    .Cast<XmlNode>()                                                                 
    .Select(x => x.Attributes["_Value"].Value).ToList();

and I get my value someValue in the first element of the list myValue . It seems like because there is only one node I'm getting this value from I should use the SelectSingleNode() method instead.

If I try this:

var myValue = someXmlDocument.SelectSingleNode("/SOME_GROUP/SOME_PLACEHOLDER/SOME_DATA/MAIN_SOME/TARGET_FIELD")
    .Cast<XmlNode>()                                                                 
    .Select(x => x.Attributes["_Value"].Value);

the code runs but I don't get anything stored in myValue as I expect.

If I leave off the ToList() in an attempt to store the value in myValue as just a string like this:

var myValue = someXmlDocument.SelectNodes("/SOME_GROUP/SOME_PLACEHOLDER/SOME_DATA/MAIN_SOME/TARGET_FIELD")
    .Cast<XmlNode>()                                                                 
    .Select(x => x.Attributes["_Value"].Value);

then I get a null reference exception like this:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Sorry if this is a stupid question, I'm just getting back into c# development. Can anyone tell me what I'm doing wrong when trying to use SelectSingleNode() or if that is even something I should be trying to use in this case?

As always, a correct, clearly explained answer will be marked as accepted and upvoted.

Thanks.

Try this...

public static void DoSelectSingleNode()
{
    var xml = "<SOME_GROUP SomeValue=\"2\"><SOME_PLACEHOLDER SOMEDateTime=\"2022-02-10T14:50:48-08\"><SOME_DATA><MAIN_SOME SomeValue=\"2\" MAINSOMEID=\"MAINSOME1\"><TARGET_FIELD MAINFIELDID=\"MAIN111\" SomeID=\"somedata\" _Value=\"someValue\"><_SUB_FIELD _Code=\"038\" _Text=\"some other data\"></_SUB_FIELD></TARGET_FIELD></MAIN_SOME></SOME_DATA></SOME_PLACEHOLDER></SOME_GROUP>";

    var xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(xml);

    var value = xmlDocument.DocumentElement.SelectSingleNode("//TARGET_FIELD").Attributes["_Value"].InnerText;

    Console.WriteLine(value);
}

... naturally, if there are more than one TARGET_FIELD then you'll only pull back one.

I find this to be a good resource when it comes to using XPath for node selections... https://devhints.io/xpath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM