473,801 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlValidatingRe ader DTD validation

I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd "> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?

XmlDocument doc = new XmlDocument();
XmlValidatingRe ader reader = new XmlValidatingRe ader(new
XmlTextReader(s tream));

reader.XmlResol ver = new MyDTDResolver() ;

reader.Validati onType = ValidationType. DTD;

doc.Load(reader );

The XML document's stream is contained in the stream variable

Here is MyDTDResolver declaration:

private class MyDTDResolver:X mlUrlResolver

{

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToRetur n)

{

return (returns a stream of the local copy of the DTD document that I
validate XML against)

}

}
Nov 12 '05 #1
18 5572


Vlad wrote:
I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd "> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?


It is a known shortcoming of the whole DTD approach that parsers do look
for a DOCTYPE declaration to validate the XML against. I have not
exhaustively looked at the .NET classes to perform DTD validation but it
might well be that you need to run your incoming XML stream through a
filter mirroring all nodes but inserting the DOCTYPE declaration at the
beginning to be able to perform the validation ágainst the DTD.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2


Vlad wrote:
I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd "> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?


It is a known shortcoming of the whole DTD approach that parsers do look
for a DOCTYPE declaration to validate the XML against. I have not
exhaustively looked at the .NET classes to perform DTD validation but it
might well be that you need to run your incoming XML stream through a
filter mirroring all nodes but inserting the DOCTYPE declaration at the
beginning to be able to perform the validation ágainst the DTD.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3
Your document has to have DOCTYPE to perform DTD validation. XmlResolver is
being used to resolve external entities, including external DTD of course.
There is not bug here.
Yan

"Vlad" <RE************ ***@comcast.net > wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd "> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute.
The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?

XmlDocument doc = new XmlDocument();
XmlValidatingRe ader reader = new XmlValidatingRe ader(new
XmlTextReader(s tream));

reader.XmlResol ver = new MyDTDResolver() ;

reader.Validati onType = ValidationType. DTD;

doc.Load(reader );

The XML document's stream is contained in the stream variable

Here is MyDTDResolver declaration:

private class MyDTDResolver:X mlUrlResolver

{

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToRetur n)

{

return (returns a stream of the local copy of the DTD document that I
validate XML against)

}

}

Nov 12 '05 #4
Your document has to have DOCTYPE to perform DTD validation. XmlResolver is
being used to resolve external entities, including external DTD of course.
There is not bug here.
Yan

"Vlad" <RE************ ***@comcast.net > wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
I have the following code:
I have a local copy of the DTD that I need to validate incoming XML
documents against.
The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd "> define.
When the following code is executed the XML gets resolved through the
XMLResolver and gets correctly validated against the locally stored DTD
file.
The problem occurs when the incoming XML contains no DOCTYPE attribute.
The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?

XmlDocument doc = new XmlDocument();
XmlValidatingRe ader reader = new XmlValidatingRe ader(new
XmlTextReader(s tream));

reader.XmlResol ver = new MyDTDResolver() ;

reader.Validati onType = ValidationType. DTD;

doc.Load(reader );

The XML document's stream is contained in the stream variable

Here is MyDTDResolver declaration:

private class MyDTDResolver:X mlUrlResolver

{

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToRetur n)

{

return (returns a stream of the local copy of the DTD document that I
validate XML against)

}

}

Nov 12 '05 #5
Vlad wrote:
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?


No Doctype - no validation. You can add Doctype to a document using a
variety of methods. XSLT is the simplest one - the following stylesheet
adds Doctype to a source document, while preserving the rest as is:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output doctype-system="myfile. dtd"/>
<xsl:template match="@*|node( )">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

With big documents though it could be quite ineffective so I'd use
custom XmlReader, which exposes synthetic Doctype node if the document
doesn't have one.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #6
Vlad wrote:
The problem occurs when the incoming XML contains no DOCTYPE attribute. The
resolver code never gets called and the validation does not occur at all.
What am I doing wrong here? Is that a bug and there is no way to enforce
the DTD even if the icoming XML file does not specify the DTD file?


No Doctype - no validation. You can add Doctype to a document using a
variety of methods. XSLT is the simplest one - the following stylesheet
adds Doctype to a source document, while preserving the rest as is:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output doctype-system="myfile. dtd"/>
<xsl:template match="@*|node( )">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

With big documents though it could be quite ineffective so I'd use
custom XmlReader, which exposes synthetic Doctype node if the document
doesn't have one.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #7
That defeats the purpose of the ValidatingReade r and makes it useless since
I have no control over the incoming files. That means that someone could
just submit an XML file without a DOCTYPE and render the whole validation
process useless.
As far as using the XSLT code to insert the DOCTYPE, I couldn't use it
because the XML files that I receive could be very large and I do not want
to regenerate the whole file just to check for its validity.
As for creating a custom XmlReader, I initially tried it but it is not that
simple since in order to fake the DOCTYPE attribute one has to first
determine that it is missing or invalid and then simulate it only in that
case. Do you have any code that could do it?

Here is what I ended up doing:
Because the ValidatingReade r does not throw any exceptions if DOCTYPE is
missing I just check XmlDocument.Doc umentType property after the document
has been read in. If it is null-I reject the file as if it has failed the
validation. It is unfortunate, since the file might actually be fully
compatible with the DTD except for the missing DOCTYPE attribute.
If you have a sample code for the custom XmlReader or any other workaround
that would ensure that the missing DOCTYPE does not effect the
ValidatingReade r functionality I would really appreciate it.
Thank you!

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:el******** ******@TK2MSFTN GP09.phx.gbl...
Vlad wrote:
The problem occurs when the incoming XML contains no DOCTYPE attribute. The resolver code never gets called and the validation does not occur at all. What am I doing wrong here? Is that a bug and there is no way to enforce the DTD even if the icoming XML file does not specify the DTD file?


No Doctype - no validation. You can add Doctype to a document using a
variety of methods. XSLT is the simplest one - the following stylesheet
adds Doctype to a source document, while preserving the rest as is:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output doctype-system="myfile. dtd"/>
<xsl:template match="@*|node( )">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

With big documents though it could be quite ineffective so I'd use
custom XmlReader, which exposes synthetic Doctype node if the document
doesn't have one.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #8
That defeats the purpose of the ValidatingReade r and makes it useless since
I have no control over the incoming files. That means that someone could
just submit an XML file without a DOCTYPE and render the whole validation
process useless.
As far as using the XSLT code to insert the DOCTYPE, I couldn't use it
because the XML files that I receive could be very large and I do not want
to regenerate the whole file just to check for its validity.
As for creating a custom XmlReader, I initially tried it but it is not that
simple since in order to fake the DOCTYPE attribute one has to first
determine that it is missing or invalid and then simulate it only in that
case. Do you have any code that could do it?

Here is what I ended up doing:
Because the ValidatingReade r does not throw any exceptions if DOCTYPE is
missing I just check XmlDocument.Doc umentType property after the document
has been read in. If it is null-I reject the file as if it has failed the
validation. It is unfortunate, since the file might actually be fully
compatible with the DTD except for the missing DOCTYPE attribute.
If you have a sample code for the custom XmlReader or any other workaround
that would ensure that the missing DOCTYPE does not effect the
ValidatingReade r functionality I would really appreciate it.
Thank you!

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!P LEASEtkachenko. com> wrote in message
news:el******** ******@TK2MSFTN GP09.phx.gbl...
Vlad wrote:
The problem occurs when the incoming XML contains no DOCTYPE attribute. The resolver code never gets called and the validation does not occur at all. What am I doing wrong here? Is that a bug and there is no way to enforce the DTD even if the icoming XML file does not specify the DTD file?


No Doctype - no validation. You can add Doctype to a document using a
variety of methods. XSLT is the simplest one - the following stylesheet
adds Doctype to a source document, while preserving the rest as is:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output doctype-system="myfile. dtd"/>
<xsl:template match="@*|node( )">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

With big documents though it could be quite ineffective so I'd use
custom XmlReader, which exposes synthetic Doctype node if the document
doesn't have one.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #9
Hi Vlad,

Based on my experience, I think you can first check the DocumentType
property of XmlDocument. If it is null, you can add a <DOCTYPE> node into
the document using XSLT and validate the XmlDocument. Although it might hit
performance when the XML file is very big, it's better than reject the file
which might be valid.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
497
by: Larry | last post by:
I believe the .Net XmlValidatingReader should fail when validating XML that contains a ComplexType element with white space when the ComplexType element has the mixed attribute set to false in the XSD used for validation. XSD fragment: <xs:element name="TRSellerBuyerData"> <xs:complexType mixed="false"> <xs:sequence>
4
2606
by: Jesse Elve | last post by:
I am using an XmlValidatingReader which uses an XSD for xml validation. The code has been performing reliably for months. Yesterday it failed for the first time with the following exception: Inner exception of type System.InvalidOperationException has occurred : The operation is not valid due to the current state of the object. [stack trace = at System.Xml.XmlValidatingReader.set_ValidationType(ValidationType
3
2232
by: Nuevo Registrado | last post by:
All, I am having a tough time figuring out how to code a simple app to read an xml file and an xsd file and validate the xml file using the xsd without using a namespace for the schema. Help? Also, is there a .NET XML FAQ? Thanks, Will
1
2643
by: George W. | last post by:
When attempting to validate an XML file, If the file is valid, it validates correctly, and it will catch most ValidationEvents without problem. However, I keep getting the following Exception: System.Xml.XmlException: The 'RDaaaa' start tag on line '4' doesn't match the end tag of 'RD' in file 'file:///C:/Work/WIMAR XML/WI_M_1.xml'. Line 4, position 475. at System.Xml.XmlTextReader.ParseTag() at...
9
1942
by: jason | last post by:
how do you use the XmlValidatingReader to validate an XML document that is passed into the XmlValidatingReader constructor? it looks like the normal process is to use an underlying reader, as follows (C#): XmlValidatingReader oMyVReader = new XmlValidatingReader(oMyReader); oMyVReader.Schemas.Add(oMySchemaCollection); oMyVReader.ValidationType = ValidationType.Schema;
6
2747
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET 2003 - Windows XP Pro PC suscessfully. But when I ran it from the command line - C:\Documents and Settings\SHC\My Documents\Visual Studio Projects\XMLdtdValidatingReader\Debug>XMLdtdValidatingReader valcanoes.xml, I got the following message in...
5
1414
by: Geoff | last post by:
I am using an XMLValidatingReader to validate an XML file received via a web service. I want to verify that the incoming file matches the XML schema. When testing the validation routine, the XMLValidatingReader correctly flags mis-matched tags such as <abc>some content</xyz> but does not catch other errors. For example, it doesn't catch tags that are not part of the schema, doesn't catch missing tags where the schema has minoccurs="1", and...
1
2116
by: Bernhard Felkel | last post by:
I have troubles validating XML files with key/keyref constraints. Here´s my schema: <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:transNS="http://Festo.Common.Translation" xmlns="http://Festo.Common.Translation" targetNamespace="http://Festo.Common.Translation" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0.1">
1
1848
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST> xml file 2
12
8569
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST>
0
9698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10520
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10293
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9102
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7591
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6830
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5617
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.