Untitled

 avatar
unknown
csharp
2 years ago
1.2 kB
4
Indexable
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

string xsdFilePath = string.Empty, xmlFilePath = string.Empty;
foreach (string arg in args)
{
    var x = arg.ToString();
    switch (Path.GetExtension(x))
    {
        case ("xsd"): xsdFilePath = arg; break;
        case ("xml"): xmlFilePath = arg; break;
    }
}
xmlFilePath = @"C:\Users\xxx\Downloads\przykladowe.xml";
xsdFilePath = @"https://www.mf.gov.pl/documents/764034/6464789/JednostkaInnaWZlotych(1)_v1-0.xsd";
XmlReader? xmlReader = null;

try
{
    var schema = new XmlSchemaSet();
    schema.Add("http://www.mf.gov.pl/schematy/SF/DefinicjeTypySprawozdaniaFinansowe/2018/07/09/JednostkaInnaWZlotych", xsdFilePath);
    xmlReader = XmlReader.Create(xmlFilePath);
    XDocument xDocument = XDocument.Load(xmlReader);
    xDocument.Validate(schema, ValidationEventHandler);
    Console.WriteLine("Wszystko OK :)");
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}
finally
{
    xmlReader?.Close();
}
Console.WriteLine("Koniec");
Console.ReadLine();

static void ValidationEventHandler(object? _, ValidationEventArgs e)
{
    throw new Exception("Znaleziony błąd: " + e.Message);
}
Editor is loading...