0

I am trying to get the value out of the aa:ConfigurationID. But I can't seem to get the namespaces right. I always get the error:

System.Xml.XPath.XPathException: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function

This is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
    xmlns:aa="http://www.axis.com/vapix/ws/action1">
    <SOAP-ENV:Header></SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <aa:AddActionConfigurationResponse>
            <aa:ConfigurationID>4</aa:ConfigurationID>
        </aa:AddActionConfigurationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And this is my code:

string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://www.w3.org/2003/05/soap-envelope""
    xmlns:aa=""http://www.axis.com/vapix/ws/action1"">
    <SOAP-ENV:Header></SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <aa:AddActionConfigurationResponse>
            <aa:ConfigurationID>4</aa:ConfigurationID>
        </aa:AddActionConfigurationResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("SOAP-ENV", "http://www.w3.org/2003/05/soap-envelope");
nsmgr.AddNamespace("aa", "http://www.axis.com/vapix/ws/action1");
            
var node = xmlDoc.DocumentElement.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/aa:AddActionConfigurationResponse/aa:ConfigurationID").InnerText;
Console.WriteLine(node);

I want to get the configurationID which is 4.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
somedude
  • 53
  • 3

1 Answers1

1

the problems was not adding the nsmgr after the path in SelectSingleNode, as explained in the comments.

correct way: var node = xmlDoc.DocumentElement.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/aa:AddActionConfigurationResponse/aa:ConfigurationID",nsmgr).InnerText;

somedude
  • 53
  • 3