From: <br...@us...> - 2008-06-19 14:24:35
|
Revision: 265 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=265&view=rev Author: brus07 Date: 2008-06-19 07:24:30 -0700 (Thu, 19 Jun 2008) Log Message: ----------- Start to work with XMLSchema Modified Paths: -------------- ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj Added Paths: ----------- ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/ ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlValidator.cs Removed Paths: ------------- ACMServer/trunk/ACMServer/Library/LibraryExtention/Xml.cs Modified: ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2008-06-18 14:29:02 UTC (rev 264) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2008-06-19 14:24:30 UTC (rev 265) @@ -35,7 +35,8 @@ <ItemGroup> <Compile Include="Log.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> - <Compile Include="Xml.cs" /> + <Compile Include="XML\Xml.cs" /> + <Compile Include="XML\XmlValidator.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Copied: ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs (from rev 264, ACMServer/trunk/ACMServer/Library/LibraryExtention/Xml.cs) =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs (rev 0) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs 2008-06-19 14:24:30 UTC (rev 265) @@ -0,0 +1,51 @@ +using System; +using System.Collections; +using System.Xml; + +namespace AcmContester.AcmLibraryExtention.XML +{ + public class Xml + { + XmlDocument document = new XmlDocument(); + + string name; + string xmlDeclaration = ""; + + public string Name + { + get + { + return name; + } + } + + public Xml(string str) + { + document.LoadXml(str); + if (document.ChildNodes[0].NodeType == XmlNodeType.XmlDeclaration) + { + xmlDeclaration = document.ChildNodes[0].OuterXml; + document.RemoveChild(document.FirstChild); + } + name = document.FirstChild.Name; + } + public override string ToString() + { + return document.OuterXml; + } + + public static string GetOuterXmlOfSingleNodeFromXpath(string fullXml, string xmlPath) + { + //XmlNodeList nodes = doc.SelectNodes("descendant::root/submit"); + XmlDocument document = new XmlDocument(); + document.LoadXml(fullXml); + return document.SelectSingleNode("descendant::" + xmlPath).OuterXml; + } + + public static bool ValidateXmlWithSchema(string xml, string schema) + { + XmlValidator validator = new XmlValidator(xml); + return validator.Validate(schema); + } + } +} Added: ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlValidator.cs =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlValidator.cs (rev 0) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlValidator.cs 2008-06-19 14:24:30 UTC (rev 265) @@ -0,0 +1,61 @@ +using System; +using System.Xml.Schema; +using System.Xml; +using System.IO; + +namespace AcmContester.AcmLibraryExtention.XML +{ + class XmlValidator + { + string xmlDocument; + + bool validity; + + public XmlValidator(string xml) + { + xmlDocument = xml; + } + + + public bool Validate(string xmlSchema) + { + XmlSchema schema = new XmlSchema(); + XmlSchema.Read(new StringReader(xmlSchema), ValidationCallBack); + + // Create the XmlSchemaSet class. + XmlSchemaSet schemas = new XmlSchemaSet(); + + // Add the schema to the collection. + schemas.Add(schema); + + // Set the validation settings. + XmlReaderSettings settings = new XmlReaderSettings(); + settings.ValidationType = ValidationType.Schema; + settings.Schemas = schemas; + settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); + + // Create the XmlReader object. + XmlReader reader = XmlReader.Create(new StringReader(xmlDocument), settings); + + validity = true; + + // Parse the file. + while (reader.Read() && validity) ; + + return validity; + } + + // Happen validation errors. + private void ValidationCallBack1(object sender, ValidationEventArgs e) + { + throw new XmlSchemaException(e.Message); + } + + // Happen validation errors. + private void ValidationCallBack(object sender, ValidationEventArgs e) + { + //Console.WriteLine("Validation Error: {0}", e.Message); + validity = false; + } + } +} Deleted: ACMServer/trunk/ACMServer/Library/LibraryExtention/Xml.cs =================================================================== --- ACMServer/trunk/ACMServer/Library/LibraryExtention/Xml.cs 2008-06-18 14:29:02 UTC (rev 264) +++ ACMServer/trunk/ACMServer/Library/LibraryExtention/Xml.cs 2008-06-19 14:24:30 UTC (rev 265) @@ -1,42 +0,0 @@ -using System; -using System.Collections; -using System.Xml; - -namespace AcmContester.AcmLibraryExtention -{ - public class Xml - { - XmlDocument document = new XmlDocument(); - - string name; - - public string Name - { - get - { - return name; - } - } - - public Xml(string str) - { - document.LoadXml(str); - //XmlNodeList nodes = doc.SelectNodes("descendant::root/submit"); - } - public override string ToString() - { - return document.OuterXml; - } - - public Xml[] GetElements() - { - XmlNodeList nodes = document.ChildNodes; - Xml[] result = new Xml[nodes.Count]; - for (int index = 0; index < nodes.Count; index++) - { - result[index] = new Xml(nodes[index].OuterXml); - } - return result; - } - } -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |