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"> </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);