none
DTD as Embedded Resource To Validate XML

    Question

  • I'm getting an error message of: 

      HResult=-2146232000
      Message=Cannot have multiple DTDs. Line 2, position 1.
      Source=System.Xml
      LineNumber=2

    When I try to use my embedded DTD to validate an XML file.

    I looked at  Validate xml through the embedded resource DTD, and am using the same basic method, but it seems they don't get the same error I do.

    I load the DTD add it to the parser context: 

                XmlParserContext parserContext = new XmlParserContext(nameTable, new XmlNamespaceManager(nameTable), "MyDocType", null, null, dtd, "file:///"+AppDomain.CurrentDomain.BaseDirectory.Replace('\\', '/'), null, XmlSpace.None);

    And even set the base URI to match the searchpath given for the DTD when I don't include the context so that it might match the DTD referenced and the DTD provided in the context.

    The XML contains: 

    <!DOCTYPE MyDocType SYSTEM "MyDocType.dtd">

    What am I doing wrong here?

    How can I get the DTD reference to match the one I have as my embedded resource?

    Thanks!
    -Baron

    Friday, June 07, 2013 3:03 AM

Answers

All replies

  • Hi Baron,

    If you have embedded the DTD file into the resource and then use Reflection to get the DTD, I think you can try to remove this line from your XML file which may cause this problem.

    <!DOCTYPE MyDocType SYSTEM "MyDocType.dtd">

    The retrieved DTD is used to create the XmlParserContext, and you can use the context to initialize the XmlValidatingReader:

    using (Stream stream = new FileStream(file, FileMode.Open))
    {
        XmlValidatingReader r = new XmlValidatingReader(stream, XmlNodeType.Element, context);
        r.ValidationType = ValidationType.DTD;
        r.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(r_ValidationEventHandler);
    
        while (r.Read())
        {
            string s = r.Value;
        }
    }
    
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 10, 2013 7:42 AM
  • Hi Chester,

    Thanks for your reply.

    I did try removing the DocType, but that also produced an error. I think it was that I had specified to use DTD processing, but didn't provide a DTD. (!) 

    So, I guess I'll just have to live with out validation. *sigh*

    ("I'm good enough, I'm smart enough, and, doggonit, people like me!" I feel better now.)

    Thanks for giving it a shot.

    -Baron

    Tuesday, June 11, 2013 12:18 AM
  • Hi Baron,

    Could you please let us know what's the error you get after removing the DocType?

    You may have a look my application to see if there are any problems.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 11, 2013 6:10 AM