473,226 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Remove XML node before validating

Hello,

I need to remove the DTD reference from an xml document, the reason
for this is that we want to validate against a schema instead (which
we have locally). It takes up to a minute to fetch all documents
referred to in the DTD, and as we have no use for them I want to
remove the reference.

I'm using XmlReaderSettings to pass in the xml document and the
schema, but when I loop through the reader it goes and tries to get
the DTD before I can remove it, so I'm assuming there's a better way
to remove it before doing the validation. I've tried using XPath but I
don't know how to find the doctype node. Is it Xpath that I should
use?

I'd be very grateful if anyone could point me in the right direction.

Thanks,

AK
Oct 27 '08 #1
4 5673
ag***********@gmail.com wrote:
I need to remove the DTD reference from an xml document, the reason
for this is that we want to validate against a schema instead (which
we have locally). It takes up to a minute to fetch all documents
referred to in the DTD, and as we have no use for them I want to
remove the reference.

I'm using XmlReaderSettings to pass in the xml document and the
schema, but when I loop through the reader it goes and tries to get
the DTD before I can remove it, so I'm assuming there's a better way
to remove it before doing the validation. I've tried using XPath but I
don't know how to find the doctype node. Is it Xpath that I should
use?
No, the XPath data model does not know any DTDs so it does certainly not
help.
If you want the XmlReader (or XmlDocument) to ignore the referenced DTD
then you can try to set the XmlResolver property (of the
XmlReaderSettings you create your XmlReader with
http://msdn.microsoft.com/en-us/libr...lresolver.aspx)
to null. That way the reader will not fetch any resources. That will
only work however if the XML document does not references any entities
defined in the DTD.
A bit more work but a more complete solution is to set the XmlResolver
to your own implementation of UrlResolver, for instance by subclassing
XmlUrlResolver, that then uses a locally cached copy of the DTDs.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 27 '08 #3
AK
On Oct 27, 2:26*pm, Martin Honnen <mahotr...@yahoo.dewrote:
No, the XPath data model does not know any DTDs so it does certainly not
help.
If you want the XmlReader (or XmlDocument) to ignore the referenced DTD
then you can try to set the XmlResolver property (of the
XmlReaderSettings you create your XmlReader withhttp://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings....)
to null. That way the reader will not fetch any resources. That will
only work however if the XML document does not references any entities
defined in the DTD.
A bit more work but a more complete solution is to set the XmlResolver
to your own implementation of UrlResolver, for instance by subclassing
XmlUrlResolver, that then uses a locally cached copy of the DTDs.
Thanks for your answer, it took so long before my post came up that I
actually thought it had gone missing at first, only noticed it now!

This is the code I'm using at the moment:

XmlDocument xdoc = new XmlDocument();
bool docIsValid = false;

try
{
xdoc.XmlResolver = null;
xdoc.Load(scorePath);

docIsValid = true;
}
catch (System.Exception ex)
{
errorList.Add(ex.Message);
}

if (docIsValid == true)
{
foreach (XmlNode node in xdoc.ChildNodes)
{
if (node.GetType().ToString().Contains("DocumentType" ))
{
// Delete it
xdoc.RemoveChild(node);
}
}

MemoryStream ms = new MemoryStream();
xdoc.Save(ms);
ms.Position = 0;
XmlReader xmlDoc = XmlReader.Create(ms);

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.XmlResolver = new LocalXmlResolver();

settings.ValidationEventHandler += new
System.Xml.Schema.ValidationEventHandler(settings_ ValidationEventHandler);

XmlSchema x =
XmlSchema.Read(Utilities.getSchemaFromResources(pv gschema),
settings_ValidationEventHandler);
settings.Schemas.Add(x);

settings.ValidationType = ValidationType.Schema;

XmlReader reader = XmlReader.Create(xmlDoc, settings);

while (reader.Read())
{

}
}

Basically I want to validate against a locally saved schema (which is
set to an embedded resource), and never validate against the DTD. The
code above is not ideal as I'm validating the xml file twice, once to
remove the DTD reference then once against the schema, however it does
avoid me having to go get all the documents referenced in the DTD
(which could take up to a minute).

Also, I've saved all the schemas referenced to in 'pvgschema' locally
and added them as embedded resources, but it doesn't seem like the
XmlResolver works as I thought as it still does an HTTP get for those
schemas on the line settings.Schemas.Add(x);.

Is there a simpler way of doing this?

Many thanks,

AK
Oct 28 '08 #4
AK
On Oct 28, 1:54*pm, AK <agda.karlb...@gmail.comwrote:
Basically I want to validate against a locally saved schema (which is
set to an embedded resource), and never validate against the DTD. The
code above is not ideal as I'm validating the xml file twice, once to
remove the DTD reference then once against the schema, however it does
avoid me having to go get all the documents referenced in the DTD
(which could take up to a minute).

Also, I've saved all the schemas referenced to in 'pvgschema' locally
and added them as embedded resources, but it doesn't seem like the
XmlResolver works as I thought as it still does an HTTP get for those
schemas on the line settings.Schemas.Add(x);.
For the second point, I had made a mistake in the resolver. It now
tries to get the embedded schema but fails as the schema has a
"xs:redefine schemaLocation" in it and I get the error message
"schemaLocation must successfully resolve if <redefinecontains any
child other than <annotation>". Is it possible to solve this or would
it be better to remove the redefine from the schema?

(Apologies if someone has already answered this - I've had troubles
seeing updates and only saw my own answer to this when I came in this
morning even if I posted it yesterday afternoon.)

Many thanks,

AK
Oct 29 '08 #5

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

Similar topics

7
by: Robert Mark Bram | last post by:
Hi All! In the code below, I am reading in an xhtml document and attempting to use selectNodes to find a <p id="rmb"> node.. But the result is: 2 - */* 0 - */p Can anyone suggest what I...
1
by: Steve S | last post by:
I need some help modifying this code. The code was orginally setup to add/remove a single table row. I modified it to add 2 (two) rows with one column that spans both rows. That part works. I...
6
by: Boobie | last post by:
Is there any other way to remove a node/element other than going into the Parent and do "removeChild" ? thanks (need to remove a div of id "foobar")
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
6
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...
2
by: William | last post by:
Have somebody a brilliant idea how to remove a nodelist in a XML document? (With C#) I have this xml file,I want delete the stock nodelist when an user don't have permissions to see stocklist of...
3
by: tom | last post by:
Hi folks Is there a piece of software or a wbsite that validates the content inside an XML node? I download an XML feed and when I export it from MSAccess to an XML file and transform it with...
11
by: David | last post by:
Hi All, I am working on a script that is theoreticaly simple but I can not get it to work completely. I am dealing with a page spit out by .NET that leaves empty tags in the markup. I need a...
0
by: AK | last post by:
Hello, I need to remove the DTD reference from an xml document, the reason for this is that we want to validate against a schema instead (which we have locally). It takes up to a minute to fetch...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.