c# : use xsd 校验 xml

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Xml;
 6 using System.Diagnostics;
 7 using System.Xml.Schema;
 8 using System.Xml.Linq;
 9 
10 namespace XsdValidator
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             
17             //Debug.Assert(false);
18             InputArgumentParser.Instance.AppName = "XsdValidator";
19             InputArgumentParser.Instance.Register("xml"typeof(String), falsefalsenull);
20             InputArgumentParser.Instance.Register("xsd"typeof(String), truetruenull);
21             try
22             {
23                 InputArgumentParser.Instance.ParseInput(args);
24             }
25             catch (Exception ex)
26             {
27                 Console.Error.WriteLine("Error: " + ex.Message);
28                 Console.WriteLine();
29                 InputArgumentParser.Instance.PrintUsage();
30                 return;
31             }
32             XmlReaderSettings settings = new XmlReaderSettings();
33             settings.ValidationType = ValidationType.Schema;
34             //http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemavalidationflags.aspx
35             settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
36             settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
37             settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
38             settings.ValidationFlags |= XmlSchemaValidationFlags.AllowXmlAttributes;
39             String strXmlFile = InputArgumentParser.Instance.GetStringArg("xml");
40 
41             foreach (String strXsdFile in InputArgumentParser.Instance.GetStringArgArray("xsd"))
42             {
43                 //String strXsdFile = @"c:\temp\unqualified_1.xsd";
44                 //String strXmlFile = @"c:\temp\test1_unqualified.xml";
45 
46                 System.Console.WriteLine("Adding XSD:" + strXsdFile);
47                 XDocument xdocXSD = XDocument.Load(strXsdFile);
48                 var e = xdocXSD.Element(XName.Get("{http://www.w3.org/2001/XMLSchema}schema"));
49                 XAttribute attr = e.Attribute("targetNamespace");
50 
51                 string namespaceUrl = null;
52                 if (attr != null)
53                     namespaceUrl = attr.Value;
54                 settings.Schemas.Add(namespaceUrl, strXsdFile);
55             }
56             settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
57 
58             XmlReader reader = XmlReader.Create(strXmlFile, settings);
59             try
60             {
61                 while (reader.Read())
62                 {/*
63                     if (reader.IsStartElement())
64                     {
65                         Console.WriteLine("Begin Element " + reader.Name);
66                     }
67                   */
68                 }
69             }
70             catch (XmlException ex)
71             {
72                 Console.Error.WriteLine("Error: [" + ex.Message + "]");
73             }
74             finally
75             {
76                 reader.Close();
77             }
78         }
79         static void settings_ValidationEventHandler(object sender
80             , System.Xml.Schema.ValidationEventArgs e)
81         {
82             Console.Error.WriteLine(e.Severity == XmlSeverityType.Error? "Error" : "Warning" + ": [" + e.Message + "]\n");
83         }
84     }//class
85 }

posted on 2012-03-25 15:43  learner  阅读(655)  评论(0编辑  收藏  举报

导航