Why does XmlDocument.GetElementById always return null?
I've got some XML (valid XHTML) that looks like this:
<html>
<head>
<script type="text/javascript">
<![CDATA[
function change_header(){
document.getElementById("myHeader").innerHTML="Nice day!";
}]]>
</script>
</head>
<body>
<开发者_如何学Go;h1 id="myHeader">Hello World!</h1>
<button onclick="change_header()">Change text</button>
</body>
</html>
And I'm trying to get the #myHeader
node using docment.GetElementById("myHeader")
but it always returns null
. Why?
I'm guessing it doesn't recognize the id
attribute as the id attribute without a DTD or something? If that's the case, how can I get it to use an HTML DTD?
It's because XmlDocument knows nothing about what an id
means. You need to include a DTD in your XHTML document. Just put the following in the beginning of your html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Example:
string html = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html><body><div id=""foo"">some content</div></body></html>";
XmlDocument document = new XmlDocument();
document.LoadXml(html);
XmlElement div = document.GetElementById("foo");
Notice that this might be a little slower because the DTD needs to be downloaded.
精彩评论