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)>