现有如下代码,用xPath来提取XmlDocument中的一个节点:
    XmlDocument xmlDocument=new XmlDocument();
    xmlDocument.Load("D:\\Category1.xml");

    XmlElement rootNode=xmlDocument.DocumentElement;
    XmlNode mapNode=rootNode.SelectSingleNode("//DATA/MAP");

    MessageBox.Show(mapNode.Name);
当Category1.xml头部包含dtd定义:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SMART_DOCUMENT SYSTEM "dtd/SMART_DOCUMENT_V_1_1_RC5.dtd">
的时候,rootNode.SelectSingleNode("//DATA/MAP")返回为null;
当Category1.xml头部没有dtd定义的时候,能够正常的返回一个MAP节点。谁有好的解决方案,使的xml文档中,包含dtd的定义,也可以用xPath在XmlDocument对象中选择对象?
高分奉送,不够再补,十万火急,在线热等。
非常感谢!

解决方案 »

  1.   

    这个问题已经问过微软的技术支持,答案使不用dtd,改用xsd。
    问题是要打开的xml是客户送过来的,不能要求客户改用xsd的啊。狂郁闷。
      

  2.   

    where is this dtd file? is it in the dtd subdirectory of the current exe? you might need to use XmlUrlResolver, seehttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassXmlResolverTopic.aspthe following works for me (based on code in the above link)1. TestDTD.xml
    <!DOCTYPE book SYSTEM 'books.dtd'>
    <book ISBN = '1-861001-57-5'>
      <title>&h;</title>
      <price>19.95</price>
      <misc>&h;</misc>
    </book>2. books.dtd (in the same directory as TestDTD.xml)<!ELEMENT book (title,price,misc)> 
    <!ATTLIST book 
       genre CDATA "novel"
       ISBN CDATA #REQUIRED>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT price (#PCDATA)>
    <!ELEMENT misc (#PCDATA)>
    <!ENTITY h "hardcover">
    <!ENTITY p "paperback">
    3. TestDTD.csusing System;
    using System.IO;
    using System.Xml;
    using System.Net;public class Sample {  public static void Main() {    // Supply the credentials necessary to access the DTD file stored on the network.
       // XmlUrlResolver resolver = new XmlUrlResolver();
       // resolver.Credentials = CredentialCache.DefaultCredentials;    // Create and load the XmlDocument.
        XmlDocument doc = new XmlDocument();
        //doc.XmlResolver = resolver;  // Set the resolver.
        doc.Load("TestDTD.xml");    // Display the entity replacement text which is pulled from the DTD file.
        XmlNode node = doc.DocumentElement.SelectSingleNode("//title");
        if (node == null)
         Console.WriteLine("null");
        else
    Console.WriteLine(node.InnerText);
      
      }
    } // End class4. output:
    hardcover
      

  3.   

    问题已经解决。是因为我的dtd中,强制定义了一个namespace,所以xpath需要加上相应的namespace设置即可。;)非常感谢你们的参与,呵呵