Why does XmlDocument.GetElementById always return null?

不打扰是莪最后的温柔 提交于 2019-12-11 04:01:33

问题


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>
        <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?


回答1:


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.



来源:https://stackoverflow.com/questions/3775785/why-does-xmldocument-getelementbyid-always-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!