Validate an XML against XSD with imports using C# - c#

I need to validate an XML documents against a set of XSD schemas. There is a top-level schema which imports other schemas, nested schemas may also import some schemas. For example, schema a.xsd imports b.xsd and c.xsd; b.xsd imports d.xsd. In this example a.xsd is a top-level schema.
I use the following code for a such validaton:
static void Main(string[] args)
{
try
{
var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Schemas.Add("targetNs", "path/to/a.xsd");
settings.ValidationEventHandler += ValidateHandler;
var reader = XmlReader.Create("path/to/file.xml", settings);
while (reader.Read()) ;
}
catch (Exception ex)
{
throw ex;
}
}
private static void ValidateHandler(object sender, ValidationEventArgs e)
{
Console.WriteLine(e.Message);
}
Although the code above implicity uses nested schemas for a valuidation, there is a problem: if some nested schema is invalid XML document it is just ignored without any exceptions.
Could you please help me with this problem?

If you need to validate the schema(s) before validating the document you can do it as follows:
try
{
using (FileStream fs = File.OpenRead("path/to/a.xsd"))
{
XmlSchema schema = XmlSchema.Read(fs, ValidateHandler);
}
}
catch (Exception e)
{
throw new Exception("Schema file is invalid. " + e.Message);
}
Then add the schema to your settings as follows:
schema.TargetNamespace = "targetNs";
settings.Schemas.Addschema(schema);

Related

How to resolve System.Xml.Schema.XmlSchemaValidationException

I am trying to validate XML using an online XSD. Here is my current code for my controller:
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using Microsoft.AspNetCore.Mvc;
namespace EINV.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class XmlController : Controller
{
[HttpPost]
public IActionResult ValidateXml2(IFormFile xmlFile, string xsdUrl)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = new XmlXsdResolver(); // Need this for resolving include and import
settings.ValidationType = ValidationType.Schema; // This might not be needed, I am using same settings to validate the input xml
//settings.DtdProcessing = DtdProcessing.Parse; // I have an include that is dtd. maybe I should prohibit dtd after I compile the xsd files.
settings.Schemas.Add(null, xsdUrl); // https://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd
settings.Schemas.Compile();
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(xmlFile.OpenReadStream(), settings, "https://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/");
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
// the following call to Validate succeeds.
document.Validate(eventHandler);
// Load the XML file into an XmlDocument
return Ok();
}
protected class XmlXsdResolver : XmlUrlResolver
{
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
return base.GetEntity(absoluteUri, role, ofObjectToReturn);
}
}
private void ValidationEventHandler(object? sender, ValidationEventArgs? e)
{
if (e?.Severity == XmlSeverityType.Error)
{
throw new Exception("XML validation error: " + e.Message);
}
}
}
}
I have referenced several other posts in trying to resolve this, such as the following:
How can I resolve the schemaLocation attribute of an .XSD when all of my .XSD's are stored as resources?
Compiling two embedded XSDs: error "Cannot resolve 'schemaLocation' attribute
Validating xml against an xsd that has include and import in c#
But always end up with the same error:
System.Xml.Schema.XmlSchemaValidationException: 'The 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2:UBLExtensions' element is not declared.'
The XML that I am using, which I downloaded into a file and upload through my SWAGGER when calling the controller, is located here: https://docs.oasis-open.org/ubl/os-UBL-2.1/xml/UBL-Invoice-2.1-Example.xml
The XSD that I am using is located here: https://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd
I think you need to set settings.Schemas.XmlResolver = new XmlUrlResolver(); as well, as the flag settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; before.
That might get you only further as I think some schemas (e.g. for signatures) are imported and not found. So in the end you will need to make sure you have local copies of those schemas and have your resolver use the local copies.

Compiling an XmlSchemaSet with imported schemas

I'm trying to create an XmlSchemaSet against the SAML 2.0 set of schema definitions, starting with the protocol schema here: https://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd
var set = new XmlSchemaSet();
XmlSchema schema;
using (var reader = XmlReader.Create(
"https://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd"))
{
schema = XmlSchema.Read(reader, (sender, e) => Console.WriteLine(e.Message));
}
set.Add(schema);
set.Compile();
When Compile is called, the following exception is thrown:
System.Xml.Schema.XmlSchemaException
Type 'urn:oasis:names:tc:SAML:2.0:assertion:EncryptedElementType' is not declared.
at System.Xml.Schema.XmlSchemaSet.InternalValidationCallback(Object sender, ValidationEventArgs e)
at System.Xml.Schema.BaseProcessor.SendValidationEvent(XmlSchemaException e, XmlSeverityType severity)
at System.Xml.Schema.BaseProcessor.SendValidationEvent(XmlSchemaException e)
at System.Xml.Schema.Compiler.CompileElement(XmlSchemaElement xe)
at System.Xml.Schema.Compiler.Compile()
at System.Xml.Schema.Compiler.Execute(XmlSchemaSet schemaSet, SchemaInfo schemaCompiledInfo)
at System.Xml.Schema.XmlSchemaSet.Compile()
at XSD.Program.Main(String[] args)
The type specified urn:oasis:names:tc:SAML:2.0:assertion:EncryptedElementType appears in the namespace imported at the top of the schema:
<import
namespace="urn:oasis:names:tc:SAML:2.0:assertion"
schemaLocation="saml-schema-assertion-2.0.xsd"/>
Using Fiddler, I can't see the application making any attempts at retrieving the imported schema.
Why don't these import statements appear to be working with the XmlSchemaSet?
The default behaviour of the XmlSchemaSet is to not try to resolve any external schemas. To this this, the XmlResolver property must be set. The go-to resolver implementation is XmlUrlResolver:
set.XmlResolver = new XmlUrlResolver();
The important thing is to set this property before adding any schemas to the set. The call to Add performs "pre-processing" on the schema, which includes resolving any import statements. Assigning the XmlResolver after calling Add appears to have no effect.
The application code needs to be:
var set = new XmlSchemaSet
{
// Enable resolving of external schemas.
XmlResolver = new XmlUrlResolver()
};
XmlSchema schema;
using (var reader = XmlReader.Create(
"https://docs.oasis-open.org/security/saml/v2.0/saml-schema-protocol-2.0.xsd"))
{
schema = XmlSchema.Read(reader, (sender, e) => Console.WriteLine(e.Message));
}
set.Add(schema);
set.Compile();
NOTE The above code still does not actually produce the desired result due to problems loading the schemas from w3.org, however the imported SAML schema is resolved successfully.

XDocument.Validate is always successful

I have a schema file which does not define any target namespaces, i.e. its definition looks like this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<!--Elements, attributes, etc. -->
</xs:schema>
The corresponding XML looks like this:
<Documents p1:CRC="0" p1:Date="1900-01-01T01:01:01+01:00" p1:Name="Test" p1:Status="new" xmlns:p1="http://www.tempuri.org/pdms.xsd" xmlns="http://www.tempuri.org/pdms.xsd">
<p1:Document p1:Date="2010-12-23T07:59:45" p1:ErrorCode="0" p1:ErrorMessage="" p1:Number="TEST00001" p1:Status="new"/>
</Documents>
Validation of this XML against the schema with e.g. Altova XMLSpy or Oxygen XML Editor fails.
However my validation in C# (.NET 4.0) does not fail. The XML is processed as an XDocument object. If I have understood correctly then XDocument.Validate() does a lax validation if no namespace is found in the schema. Thus the validation does not fail. But then how can I implement a "strict" validation for XDocument?
This is how I try to validate the XML:
public static void ValidateXml(XDocument xml, string xsdFilename) {
XmlReaderSettings settings = new XmlReaderSettings();
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(string.empty, xsdFilename);
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
xml.Validate(schemaSet, ValidationCallback);
}
private static void ValidationCallback(object sender, ValidationEventArgs args) {
if (args.Severity == XmlSeverityType.Warning) {
// Do warning stuff...
} else if (args.Severity == XmlSeverityType.Error) {
// Do error stuff...
}
}
I am not sure it is possible to use the Validate method; if you use a validating XmlReader over the XDocument where ValidationFlags are set up to emit validation warnings, as in
XDocument doc = XDocument.Load("../../XMLFile1.xml");
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "../../XMLSchema1.xsd");
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ValidationType = ValidationType.Schema;
xrs.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
xrs.Schemas = schemaSet;
xrs.ValidationEventHandler += (o, s) => {
Console.WriteLine("{0}: {1}", s.Severity, s.Message);
};
using (XmlReader xr = XmlReader.Create(doc.CreateReader(), xrs))
{
while (xr.Read()) { }
}
then the ValidationEventHandler does emit a warning for each node it does not find schema information for. So your ValidationEventHandler could check for such warnings. But you might as well simply compare the doc.Root.Name.Namespace with the target namespace of the schemas you have before calling the Validate method.

Modifying App.Config file at the time of installation using c#

<configuration>
<configSections>
<section name="ADMIN" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<User>
<add key="ExtendTime" value="20"/>
<add key="Name" value="sss"/>
</User>
<configuration>
i have to remove first child element in user config section i.e . Reply me if you have any idea for this.
i am using
Configuration config = ConfigurationManager.OpenExeConfiguration(Context.Parameters["assemblypath"]);
ConfigurationSection section = config.GetSection("USER");
This article may have what you're looking for : http://raquila.com/software/configure-app-config-application-settings-during-msi-install/
Excerpt from article:
string exePath = string.Format("{0}MyWindowsFormsApplication.exe", targetDirectory);
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
config.AppSettings.Settings["Param1"].Value = param1;
config.AppSettings.Settings["Param2"].Value = param2;
config.AppSettings.Settings["Param3"].Value = param3;
config.Save();
EDIT: Adding additional code sample and blog reference: http://ryanfarley.com/blog/archive/2004/07/13/879.aspx
using System;
using System.Xml;
using System.Configuration;
using System.Reflection;
//...
public class ConfigSettings
{
private ConfigSettings() {}
public static string ReadSetting(string key)
{
return ConfigurationSettings.AppSettings[key];
}
public static void WriteSetting(string key, string value)
{
// load config document for current assembly
XmlDocument doc = loadConfigDocument();
// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
try
{
// select the 'add' element that contains the key
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[#key='{0}']", key));
if (elem != null)
{
// add value for key
elem.SetAttribute("value", value);
}
else
{
// key was not found so create the 'add' element
// and set it's key/value attributes
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", value);
node.AppendChild(elem);
}
doc.Save(getConfigFilePath());
}
catch
{
throw;
}
}
public static void RemoveSetting(string key)
{
// load config document for current assembly
XmlDocument doc = loadConfigDocument();
// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");
try
{
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
else
{
// remove 'add' element with coresponding key
node.RemoveChild(node.SelectSingleNode(string.Format("//add[#key='{0}']", key)));
doc.Save(getConfigFilePath());
}
}
catch (NullReferenceException e)
{
throw new Exception(string.Format("The key {0} does not exist.", key), e);
}
}
private static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(getConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
}
private static string getConfigFilePath()
{
return Assembly.GetExecutingAssembly().Location + ".config";
}
}
Then you would use it like this:
// read the Test1 value from the config file
string test1 = ConfigSettings.ReadSetting("Test1");
// write a new value for the Test1 setting
ConfigSettings.WriteSetting("Test1", "This is my new value");
// remove the Test1 setting from the config file
ConfigSettings.RemoveSetting("Test1");
I have come to the conclusion that it is not possible to access a custom configuration section during installation using:
MyCustomConfigurationSection section = (MyCustomConfigurationSection)config.GetSection("MyCustomConfigurationSection");
When the MSI package is installed, the program executed is the Windows Install(MsiExec), not the program which contains the installer class.
'%windir%\system32\msiexec.exe
In order to access the config we need to workaround this issue either by using the context:
Configuration config = ConfigurationManager.OpenExeConfiguration(this.Context.Parameters["assemblypath"]);
Or, by using reflection retrieve the location of the executing assembly:
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
As Chuck suggested, you can access the AppSettings and modify them:
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings["Environment"].Value = _Environment;
config.Save();
This is fine, because the installer knows exactly how to deal with
System.Configuration.AppSettingsSection
because that library is part of .NET. However, when it comes to a custom section, the installer needs to know how to deal with that custom config. More than likely, you have it in a class library (in a DLL) that is referenced by your application and that DLL has now been installed in the install directory.
The problem is, as we know from above, MSIExec.exe isn't running in the context of that directory, so the install fails, when it can't find the appropriate DLL in system32, it throws the error:
An error occurred creating the
configuration section handler for
'XXX': Could not load file or assembly
'XXX.dll' or one of its dependencies.
The system cannot find the file
specified.
The only way therefore, to access the custom config, is to treat the config file as an XML document, and edit it using traditional XML management tools:
// load the doc
XmlDocument doc = new XmlDocument();
doc.Load(Assembly.GetExecutingAssembly().Location + ".config");
// Get the node
XmlNode node = doc.SelectSingleNode("//MyCustomConfigurationSection");
// edit node here
// ...
// Save
doc.Save(Assembly.GetExecutingAssembly().Location + ".config");
This technique is described on Ryan Farley's blog, as Chuck pointed out in the comments to his original answer.
Good news! I have found a way how to work-around this problem.
Solution is to intercept loading of assembly and return one we have. To do so
ResolveEventHandler handler = new ResolveEventHandler(CurrentDomain_AssemblyResolve);
AppDomain.CurrentDomain.AssemblyResolve += handler;
try
{
section = config.GetSection("mySection") as MySection;
}
catch(Exception)
{
}
AppDomain.CurrentDomain.AssemblyResolve -= handler;
and
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name == "Dead.Beef.Rocks")
{
return typeof(MySection).Assembly;
}
return null;
}

how can I validate dot net application config file(ex, app.exe.config) on console?

is there any tool to validate configuration file?
Well, basically your app is the validator - if the config file is not valid, you'll get an exception when starting up. Other than that, I'm not aware of any out-of-the-box validation support for app.config files.
In your directory C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas, you'll find some files called DotNetConfig.xsd / DotNetConfig20.xsd - those are Microsoft-supplied XML schema files, which you could easily use to validate any other config files you might have for validity.
The basic structure for programmatically validating your configs would be something like this:
using(StreamReader xsdReader = new StreamReader(xsdFileName))
{
XmlSchema Schema = new XmlSchema();
Schema = XmlSchema.Read(xsdReader, new ValidationEventHandler(XSDValidationEventHandler));
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
ReaderSettings.ValidationType = ValidationType.Schema;
ReaderSettings.Schemas.Add(Schema);
ReaderSettings.ValidationEventHandler += new ValidationEventHandler(XMLValidationEventHandler);
using(XmlTextReader xmlReader = new XmlTextReader(xmlFileName))
{
XmlReader objXmlReader = XmlReader.Create(xmlReader, ReaderSettings);
while (objXmlReader.Read())
{ }
}
}
Console.WriteLine("Successful validation completed!");
What you need to do now is supply event handlers for those event that get raised when something in the validation goes wrong - that's about it! :-)
Very old question but I had the same question and here is my setup (.net framework 3.5 and up):
I created a console project named 'ConfigurationValidator' :
static void Main(string[] args)
{
try
{
string xsdFileName = ConfigurationManager.AppSettings["configXsdPath"];
string xmlFileName = args[0];
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, xsdFileName);
XDocument doc = XDocument.Load(xmlFileName);
string validationMessage = string.Empty;
doc.Validate(schemas, (sender, e) => { validationMessage += e.Message + Environment.NewLine; });
if (validationMessage == string.Empty)
{
Console.WriteLine("CONFIG FILE IS VALID");
}
else
{
Console.WriteLine("CONFIG FILE IS INVALID : {0}", validationMessage);
}
}
catch(Exception ex)
{
Console.WriteLine("EXCEPTION VALIDATING CONFIG FILE : {0}", ex.Message);
}
}
and the following app.config :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="configXsdPath" value="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Xml\Schemas\DotNetConfig35.xsd"/>
</appSettings>
</configuration>
For each project of the solution add post build event command, for example :
when validation succeeds i get the following output :
when validation fails i get an output like so:

Categories

Resources