How to dertimate if a xml file is valid

A

ad

I have a xml, with a DTD to valid it:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE MyHealthData SYSTEM "Health.dtd">

How can I dertimate if a xml file is valid by program?
 
J

Jon Skeet [C# MVP]

ad said:
I have a xml, with a DTD to valid it:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE MyHealthData SYSTEM "Health.dtd">

How can I dertimate if a xml file is valid by program?

Create an instance of XmlValidatingReader, set the ValidationType
appropriately, and subscribe to the (somewhat oddly named)
ValidationEventHandler event. Then pass the reader into
XmlDocument.Load.
 
A

ad

The process is so difficult for me.
I have study the example about XmlValidatingReader in MSDN.
But the result is always success even there are some incompatible between
the DTD and XML..

Could you give me an example?
 
J

Jon Skeet [C# MVP]

ad said:
The process is so difficult for me.
I have study the example about XmlValidatingReader in MSDN.
But the result is always success even there are some incompatible between
the DTD and XML..

Could you give me an example?

Better, I'd suggest you give me an example of what you've already got,
and we'll work from there.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
A

ad

I have complete a example code for msdn
http://support.microsoft.com/kb/307379/en-us?ln=en-us&sd=gn&fr=0
But in this example. It can just determinate whether the xml is valid.
But if my xml is not will-formatted (like I change the end tag of
ProductName ot ProductXX)
The program still say that the xml is valid.
Can we determinate a xml document is valid and well-formatted

I have open a new session in this newsgroup, could help me in that?

----------------------------------------------------------------------------
--------------------------
class Program
{
private static bool isValid = true; // If a validation error occurs,
// set this flag to false in the
// validation event handler.
static void Main(string[] args)
{
XmlTextReader r = new
XmlTextReader("d:\\data\\WinNet\\XmlVal\\Product.xml");

XmlValidatingReader v = new XmlValidatingReader(r);
v.ValidationType = ValidationType.DTD;
v.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);

v.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);

while (v.Read())
{

}
v.Close();

// Check whether the document is valid or invalid.
if (isValid)
Console.WriteLine("Document is valid");
else
Console.WriteLine("Document is invalid");
Console.ReadLine();



}
public static void MyValidationEventHandler(object
sender,ValidationEventArgs args)
{
isValid = false;
Console.WriteLine("Validation event\n" + args.Message);
}
}

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE Product SYSTEM "Product.dtd">
<Product ProductID="123">
<ProductName>Rugby jersey</ProductName>
</Product>



<!ELEMENT Product (ProductName)>
<!ATTLIST Product ProductID CDATA #REQUIRED>
<!ELEMENT ProductName (#PCDATA)>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top