equal
deleted
inserted
replaced
|
1 using System; |
|
2 using System.Collections.Generic; |
|
3 using System.Linq; |
|
4 using System.Text; |
|
5 using System.Xml; |
|
6 using System.Xml.Schema; |
|
7 |
|
8 |
|
9 namespace XmlValidator |
|
10 { |
|
11 class Validator |
|
12 { |
|
13 |
|
14 static void Main(string[] args) |
|
15 { |
|
16 string dtdFile = string.Empty; |
|
17 string xmlFile = string.Empty; |
|
18 |
|
19 if (args.Length == 2) |
|
20 { |
|
21 dtdFile = args[0]; |
|
22 xmlFile = args[1]; |
|
23 } |
|
24 else |
|
25 { |
|
26 Console.WriteLine("Usage: XmlValidator.exe dtdfile.dtd xmlfile.xml"); |
|
27 return; |
|
28 } |
|
29 try |
|
30 { |
|
31 XmlReaderSettings settings = new XmlReaderSettings(); |
|
32 settings.ValidationType = ValidationType.DTD; |
|
33 settings.ValidationEventHandler += XmlValidationEventHandler; |
|
34 settings.ProhibitDtd = false; |
|
35 XmlReader xmlReader = XmlReader.Create(xmlFile, settings); |
|
36 |
|
37 while (xmlReader.Read()) |
|
38 { |
|
39 // Do nothing |
|
40 } |
|
41 xmlReader.Close(); |
|
42 } |
|
43 catch (XmlException /* xmle */) |
|
44 { |
|
45 Console.Write("Not valid"); |
|
46 return; |
|
47 } |
|
48 catch (System.IO.FileNotFoundException ex) |
|
49 { |
|
50 Console.WriteLine("File not found!"); |
|
51 Console.WriteLine(ex.Message); |
|
52 return; |
|
53 } |
|
54 |
|
55 if (isValid) |
|
56 { |
|
57 Console.Write("Valid"); |
|
58 } |
|
59 else |
|
60 { |
|
61 Console.Write("Not valid"); |
|
62 } |
|
63 |
|
64 } |
|
65 |
|
66 public static void XmlValidationEventHandler(object sender, ValidationEventArgs args) |
|
67 { |
|
68 isValid = false; |
|
69 // Console.WriteLine("Validation event\n" + args.Message); |
|
70 } |
|
71 private static bool isValid = true; |
|
72 } |
|
73 } |