0

I've got some problems finding a single node in my xhtml document. I'm loading a XHTML string in a XmlDocument because i'm not allowed to use the HTML Agility Pack.

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

There are different cases in which I need to search different nodes. For example

 selectedNode = xmlDoc.SelectSingleNode("//*[@class='" + classText + "'][@title='" + titleText + "']");

is returning me the first node that has a special class and a special title Attribute, no matter what element it is. That is working just fine!

But in one case i need to get a specific element type 'input' that has a special name Attribute. I've tried several different XPATH but after some Google Research this should do it. But it doesn't and some people in my company including me have no clue why as by definition it should work...

selectedNode = xmlDoc.SelectSingleNode("//input[@name='" + nameText + "']");

I can't select a full path because the xhtml structur inside of <form> can be different. This is a part of the XHTML Document:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Online</title>
</head>

<body onload="javascript:setup();">

<form id="daten" action="{{{Intern_FORMACTION}}}" method="post">
 <div>
  <input name="Z0000" type="hidden" value="7" />
  <input name="xt" type="hidden" value="" />
  <input name="XE001" type="hidden" value="" />
  <input name="XE002" type="hidden" value="" />
  <input name="mv" type="hidden" value="1" />
  <div class="tabBar">
                  <div class="tabBarContent">
                     <div class="block8">&#160;</div>
                  </div>
         </div>
        </div>
</form>
</body>
</html>

For example i want to get this node: <input name="XE001" type="hidden" value="" />

But it is always returning null.

If i run this e.g. in XPATH Tester it's all working and i get the correct input element. It just isn't working in code..

Any ideas are highly appreciated!

[UPDATE #1]

Tried to set the default namespace to the xhtml element

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("", xmlDoc.DocumentElement.NamespaceURI);

DefaultNameSpace in XmlDocument is now http://www.w3.org/1999/xhtml which should be right i guess.

However all these tests still return null.

xmlDoc.SelectSingleNode("//input[@name='" + nameText + "']", nsmgr);
xmlDoc.SelectSingleNode("//html/input[@name='" + nameText + "']", nsmgr);
xmlDoc.SelectSingleNode("/html/input[@name='" + nameText + "']", nsmgr);
xmlDoc.SelectSingleNode("//html:input[@name='" + nameText + "']", nsmgr);
Relax
  • 283
  • 2
  • 15
  • The element input is in the xhtml namespace. So either declare the namespace or wild card the element e.g. //*:input – chrisis Oct 25 '18 at 08:05
  • System.Xml.XPath.XPathException: "'//*:input[@name='A07']' is a invalid Token." and //*/input results in null again – Relax Oct 25 '18 at 08:21
  • That looks like you are trying to nest `'` quotes, can that be right? – Mr Lister Oct 25 '18 at 09:52
  • That ' quotes just get added by the exception error, the code itself is //*:input[@name='A07'] as provided by chrisis. You can see the query itself in my code above – Relax Oct 25 '18 at 09:57
  • See [Using Xpath With Default Namespace in C#](https://stackoverflow.com/questions/585812/using-xpath-with-default-namespace-in-c-sharp) and set the xhtml namespace as the default namespace. – chrisis Oct 25 '18 at 12:24
  • I've updated my post with my try on the Default Namespace stuff.. cant make it work however. Thank you anyway – Relax Oct 25 '18 at 13:37
  • `xmlDoc.SelectSingleNode("//html:input[@name='" + nameText + "']", nsmgr);` is what you want. But you must assign the `html` prefix to the `http://www.w3.org/1999/xhtml` namespace in your `nsmgr`. – Alohci Nov 01 '18 at 01:17

0 Answers0