Skip to content

Commit 0b494dd

Browse files
author
Fabio Gomes
committed
Commit inicial
0 parents  commit 0b494dd

5 files changed

+215
-0
lines changed

Program.cs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Collections;
6+
using System.Data;
7+
using System.IO;
8+
using System.Xml;
9+
using System.Xml.Schema;
10+
11+
namespace XMLSchemaValidator
12+
{
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
if (XML_XSD_Validator.Validate(@"D:\Dev\Monde\Monde\lib-nfse\dunit\NC.XML")) //@"C:\Users\Fábio\Desktop\Abrasf\Exemplos\EnviarLoteRpsEnvio.xml"))
18+
Console.WriteLine("Validação OK");
19+
else
20+
Console.WriteLine(XML_XSD_Validator.GetError());
21+
22+
Console.ReadKey();
23+
}
24+
}
25+
26+
public static class XML_XSD_Validator
27+
{
28+
static int numErrors = 0;
29+
static string msgError = "";
30+
31+
public static bool Validate(string XMLPath)
32+
{
33+
ClearErrorMessage();
34+
try
35+
{
36+
XmlSchemaSet schema = new XmlSchemaSet();
37+
38+
XmlTextReader tr = new XmlTextReader(GetFileStream(@"C:\Users\Fábio\Desktop\Abrasf\Schemas\servico_enviar_lote_rps_envio.xsd"));
39+
schema.Add(null, tr);
40+
tr = new XmlTextReader(GetFileStream(@"C:\Users\Fábio\Desktop\Abrasf\Schemas\tipos_simples.xsd"));
41+
schema.Add(null, tr);
42+
tr = new XmlTextReader(GetFileStream(@"C:\Users\Fábio\Desktop\Abrasf\Schemas\tipos_complexos.xsd"));
43+
schema.Add(null, tr);
44+
tr = new XmlTextReader(GetFileStream(@"C:\Users\Fábio\Desktop\Abrasf\Schemas\xmldsig-core-schema20020212.xsd"));
45+
schema.Add(null, tr);
46+
47+
XmlReaderSettings settings = new XmlReaderSettings();
48+
settings.ValidationType = ValidationType.Schema;
49+
settings.Schemas.Add(schema);
50+
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
51+
settings.ValidationEventHandler += new ValidationEventHandler(ErrorHandler);
52+
XmlReader reader = XmlReader.Create(XMLPath, settings);
53+
54+
// Validate XML data
55+
while (reader.Read());
56+
reader.Close();
57+
58+
// exception if validation failed
59+
if (numErrors > 0)
60+
throw new Exception(msgError);
61+
62+
return true;
63+
}
64+
catch (Exception e)
65+
{
66+
Console.WriteLine("Exception: " + e.Message);
67+
msgError = "Validation failed\r\n" + msgError;
68+
return false;
69+
}
70+
}
71+
72+
private static void ErrorHandler(object sender, ValidationEventArgs args)
73+
{
74+
msgError = msgError + "\r\n" + args.Message;
75+
numErrors++;
76+
}
77+
78+
public static string GetError()
79+
{
80+
return msgError;
81+
}
82+
83+
private static void ClearErrorMessage()
84+
{
85+
msgError = "";
86+
numErrors = 0;
87+
}
88+
89+
private static Stream GetFileStream(string filename)
90+
{
91+
return new FileStream(filename, FileMode.Open);
92+
}
93+
}
94+
95+
96+
}

Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("XMLSchemaValidator")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("XMLSchemaValidator")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("4a516871-aeca-4951-b9ff-8dd0e03fbcb4")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

XMLSchemaValidator.csproj

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{7F12F8A0-234F-4B9B-B40A-E2BFA21EE54C}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>XMLSchemaValidator</RootNamespace>
12+
<AssemblyName>XMLSchemaValidator</AssemblyName>
13+
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>
15+
</TargetFrameworkProfile>
16+
<FileAlignment>512</FileAlignment>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
19+
<PlatformTarget>x86</PlatformTarget>
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
29+
<PlatformTarget>x86</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="System" />
39+
<Reference Include="System.Core" />
40+
<Reference Include="System.Xml.Linq" />
41+
<Reference Include="System.Data.DataSetExtensions" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="app.config" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
</Project>

XMLSchemaValidator.sln

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual C# Express 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XMLSchemaValidator", "XMLSchemaValidator.csproj", "{7F12F8A0-234F-4B9B-B40A-E2BFA21EE54C}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|x86 = Debug|x86
9+
Release|x86 = Release|x86
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{7F12F8A0-234F-4B9B-B40A-E2BFA21EE54C}.Debug|x86.ActiveCfg = Debug|x86
13+
{7F12F8A0-234F-4B9B-B40A-E2BFA21EE54C}.Debug|x86.Build.0 = Debug|x86
14+
{7F12F8A0-234F-4B9B-B40A-E2BFA21EE54C}.Release|x86.ActiveCfg = Release|x86
15+
{7F12F8A0-234F-4B9B-B40A-E2BFA21EE54C}.Release|x86.Build.0 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

app.config

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0"?>
2+
<configuration>
3+
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

0 commit comments

Comments
 (0)