Created
September 4, 2015 12:34
-
-
Save taisyo7333/c5ef9326195350ffc748 to your computer and use it in GitHub Desktop.
C# XML Schema 検証
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool XmlValidTest( string path_xml , string path_schema ) | |
{ | |
using (XmlTester tester = new XmlTester()) | |
{ | |
if( tester.Test(path_schema, path_xml) ) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Xml; | |
using System.Xml.Schema; | |
namespace XmlValidator | |
{ | |
/// <summary> | |
/// XML Validation Test by XML Schema | |
/// </summary> | |
class XmlTester : IDisposable | |
{ | |
#region Field | |
private int error_count = 0; | |
private string error_messages = ""; | |
private bool disposed = false; | |
#endregion | |
#region Property | |
/// <summary> | |
/// property Message | |
/// </summary> | |
public string Message | |
{ | |
get { return error_messages; } | |
} | |
#endregion | |
/// <summary> | |
/// Constructor | |
/// </summary> | |
public XmlTester() | |
{ | |
} | |
/// <summary> | |
/// Destoructor | |
/// </summary> | |
~XmlTester() | |
{ | |
Dispose(false); | |
} | |
/// <summary> | |
/// Public Implementation of Dispose pattern callable by consumers. | |
/// Implement "Dispose Pattern" | |
/// </summary> | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
/// <summary> | |
/// Internal method's implementation of Dispose pattern. | |
/// </summary> | |
/// <param name="disposing"></param> | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (disposed) | |
return; | |
if (disposing) | |
{ | |
// Free any other managed objects here. | |
// | |
} | |
// Free any unmanaged objects here. | |
// | |
disposed = true; | |
} | |
/// <summary> | |
/// write out common information as trace. | |
/// </summary> | |
/// <param name="ex">threw exception</param> | |
private void WriteException(Exception ex) | |
{ | |
error_count++; | |
Trace.WriteLine(ex.TargetSite); | |
Trace.WriteLine(ex.StackTrace); | |
Trace.WriteLine(ex.Message); | |
error_messages += ex.Message; | |
} | |
/// <summary> | |
/// EventHanlder . | |
/// This is called when Illegal condition occurs. | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="args"></param> | |
public void ValidationHandler(object sender, ValidationEventArgs args) | |
{ | |
error_count++; | |
switch (args.Severity) | |
{ | |
case XmlSeverityType.Error: | |
error_messages += args.Message; | |
Trace.WriteLine(args.Message); | |
break; | |
case XmlSeverityType.Warning: | |
error_messages += args.Message; | |
Trace.WriteLine(args.Message); | |
break; | |
default: | |
error_messages += args.Message; | |
Trace.WriteLine(args.Message); | |
break; | |
} | |
} | |
/// <summary> | |
/// Test a xml file if it is valid is or not. | |
/// </summary> | |
/// <param name="path_schema">path xml schema file(.xsd)</param> | |
/// <param name="path_xml">path xml file(.xml)</param> | |
/// <returns>true = valid , false = invalid.</returns> | |
public bool Test(string path_schema, string path_xml) | |
{ | |
try | |
{ | |
using (StreamReader sr = new StreamReader(path_xml)) | |
{ | |
return Test(path_schema, sr); | |
} | |
} | |
catch (System.IO.FileNotFoundException ex) | |
{ | |
WriteException(ex); | |
} | |
catch (IOException ex) | |
{ | |
WriteException(ex); | |
} | |
catch (Exception ex) | |
{ | |
WriteException(ex); | |
} | |
return false; | |
} | |
/// <summary> | |
/// Test a xml file if it is valid or not. | |
/// </summary> | |
/// <param name="path_schema">path xml schema file(.xsd)</param> | |
/// <param name="trXml">xml text reader</param> | |
/// <returns></returns> | |
private bool Test(string path_schema, TextReader trXml) | |
{ | |
try | |
{ | |
XmlTextReader reader_schema = new XmlTextReader(path_schema); | |
XmlSchema schema = XmlSchema.Read(reader_schema, ValidationHandler); | |
XmlReaderSettings settings = new XmlReaderSettings(); | |
settings.Schemas.Add(schema); | |
settings.ValidationType = ValidationType.Schema; | |
XmlReader reader_xml = XmlReader.Create(trXml, settings); | |
XmlDocument xdoc = new XmlDocument(); | |
xdoc.Load(reader_xml); | |
xdoc.Validate(ValidationHandler); | |
return true; | |
} | |
catch (System.IO.FileNotFoundException ex) | |
{ | |
// File Not Found | |
WriteException(ex); | |
} | |
catch (XmlSchemaException ex) | |
{ | |
WriteException(ex); | |
} | |
catch (XmlException ex) | |
{ | |
WriteException(ex); | |
} | |
catch(Exception ex) | |
{ | |
WriteException(ex); | |
} | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment