Question

i am using a xpathdocument to get some values by their xpath expression, which works until it comes to namespaces.

The xml:

?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="vsstyle_xml.cgi" type="text/css"?>
<vs120data version="0x1210019" build="21.0.117" label="D21.00.117SAEN060223"   
xmlns:html="http://www.w3.org/1999/xhtml" SIMATIC_VS_Escaped="1">
VS120 Data from 192.168.0.43 (MAC: 08:00:06:71:8C:EE) Time Sync Info: Source was PC:   
PC=192.168.0.1 - TZ is GMT/UTC +1h. Last Synchronization: Feb 27, 2006, 16:41:08.   
Current Timestamp: Feb 27, 2006, 16:44:14.  HWVers: 0x34<html:title>VS120   
Data from 192.168.0.43 (MAC: 08:00:06:71:8C:EE)  at Feb 27,    2006, 16:44:14.   
</html:title>   
<Standard-Parameters>      
Standard-Parameters   
  <Startup-Request> 

...

<Images>
Images
  <Scene>
  Scene<html:img alt="html:img" src="data:image/png;base64...."

'

The c# code:

XmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable);      
mgr.AddNamespace("html", "http://www.w3.org/1999/xhtml");   
XPathNavigator hc = nav.SelectSingleNode("//html:img", mgr);  

i only get empty node here.

When i use the sandart xpath parsing by the xpath expression

parseXPath("/vs120data/Model/Images/Scene/html:img/@src");

i get the namespace problem, that some prefix stuff is missing.

Was it helpful?

Solution

Obviously, if you need to get to the src attribute, change:

nav.SelectSingleNode("//html:img", mgr)

to:

nav.SelectSingleNode("//html:img/@src", mgr)

As you need not the attribute itself, but its string value, supposing the type of nav is IXpathNavigable, use:

nav.Evaluate("string(//html:img/@src)", mgr)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top