validating against DTD

k3n51mm

Active member
Joined
Mar 7, 2007
Messages
40
Programming Experience
3-5
I am trying to import XML files whose schema is defined in a DTD. Initial research indicates I must convert the DTD to an .xsd file, but when I do so in XMLSpy it gives me a warning that it may not work right: "Some of 'include' and/or 'import' and/or 'redefine' statements have no schemaLocation attribute and will be ignored!"
Besides the fact that the error I get from XMLSpy has bad linguistic syntax, it tells me nothing useful.
QUESTIONS:
1) How do I do this in VB.Net 2005?
2) Just in case I actually CAN use the .dtd (which I much prefer since it's the client's core validation document), how can I use it as a compiled resource from my application so the user doesn't have to browse to the dtd every time they want to import a file?
thanks
 
VB.Net 2005: DTD as embedded resource

I'm using the following code to validate an importing XML file:

VB.NET:
Dim xrsFile As New XmlReaderSettings()
xrsFile.ProhibitDtd = False
xrsFile.ValidationType = ValidationType.DTD
Dim rdr As XmlReader = XmlReader.Create(xmlFile, xrsFile)
While rdr.Read()
' ... do stuff
End While
rdr.Close()
I am guessing that if the DTD is not in the same directory as the imported XML file, then it will not validate because the application will not know where to look for the DTD - this has happened on other projects.

Since all XML files handled by this application must adhere to the same DTD, how can I reference the DTD as an embedded resource for purposes of validation?
Failing that, how can I at least specify where to look for the DTD?

There doesn't seem to be any option at all listed in the documentation at MSDN.
 
Last edited by a moderator:
The DTD file can be any path, local filesystem path, network path or internet location path.

If you choose to keep it as an application resource you can use the My.Resources.ResourceManager.GetStream("name") to to load it from the stream. As you see the XmlReader.Create method includes 12 overloads including several to load from stream.
 
Suggesting GetStream for this text resource was a little hasty, there are some options to solve this, you could temporarily store the resource to file path, see other options here discussing Xsl in resource which is also a Xml file like DTD http://www.vbdotnetforums.com/showthread.php?p=34081
 
Thanks for your response John!

Sorry you had to move two of my posts in the last 2 days - I failed (twice!) to see the XML forum section.
___________________________________________

I have used an xsl stylesheet as a compiled resource before, in another project. Your link points to that as a solution to another issue, but I think it's unrelated to what I have here. Actually a .dtd is NOT a valid xml file itself (this has always been an interesting fact IMO), so it cannot be loaded into a memorystream using an XMLReader instance.

I have tried using XMLSpy to convert this DTD to an .xsd file (which is what I assume you meant - but typed 'xsl' in your message), but it throws a warning, saying the resultant .xsd file may not be correct because some of the elements didn't have a schemaLocation attribute.

This error from XMLSpy seemed more detailed than I wanted to get at this time since I couldn't find an answer as to whether or not it really meant anything important. So, I decided to stay with the DTD. Apparently, this is also more detailed than I wanted to get. :)

At any rate, I couldn't figure out how to overload the XML Reader parameters to specify a DTD, whether from a disk path or an embedded resource. That's pretty much all I need. Writing the DTD to disk temporarily is certainly a kludge, but I'll do it if I have to. I just had trouble believing it would be so roundabout, and I suspected I was missing something.

Any other thoughts?
 
I always thought DTD was xml, but never used them, a quick try load now shows it isn't xml after all, oh well. About converting DTD to XSD I don't think there is builtin framework functionality for it.

I just found that DTD is either inline (inside the Xml document) or referenced by a doctype directive in the xml file, it seems like the typical thing to be referencing to a static webadress? Anyway I misunderstood something here about the whole thing, thought it function like the XSD... DTD doesn't seem very dynamically useful.
 
Back
Top