tags:

views:

36

answers:

1

consider this:

I'm inside a (selfbuilt) XML Editor and am about to add a Child to an XmlNode. How do I know which types of children are valid according to a DTD.

it's a behaviour like Intellisense. I couldn't find any .NET classes for "parsing" the DTD.

How would i go about this?

A: 

Unfortunately, the DOM Level 1 Core standard which XmlDocument implements does not provide any access to <!ELEMENT> and <!ATTLIST> declarations in a DTD (internal subset or, if configured to read it, external DTD).

You do get Document.DocumentType.Entities, which tells you what general entities (&something;) were defined in the DTD, and Notations, which is largely useless, but not Elements or Attlists. Whilst there exist DOMs that will retain this information, I'm not aware of any for .NET (unless you want to go running pxdom through IronPython, which would probably be a bit of a pain and not at all fast) and nothing that integrates with System.Xml.

You could possibly hook up saxdotnet, using a declHandler (looks like eg. ExpatReader.SetDeclHandlerForParsing in saxdotnet) to pick up these declarations. Either as a separate parse process for the internalSubset/systemId DTD, or as a replacement for .NET's own parsing, converting the event stream in an XmlDocument manually.

bobince
yeah I noticed Document.DocumentType, which wasn't of any help. this seems strange though. How is XmlReader validating against the DTD? haven't had any time yet to start up Reflector to see...
redoced
Don't know but it doesn't seem to be part of the publically-available API. (It's quite common for XML parsers to not provide access to the DTD declarations, unfortunately.)
bobince
i'm going for xsd! seems better and .net does actually support what i want!
redoced
Yeah, DTD is a pretty old-and-nasty method of validation... if you can migrate to one of the more modern schema languages that's definitely preferable.
bobince