How to

validate XML file by XSD schema

Published: 28. December 2014 | Updated: 28. December 2014
License: Microsoft Public License (MS-PL)
Categories: Framework » XML
Tags: C# Validation XML
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at

Import namespaces

using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;


var schemas = new XmlSchemaSet();

// add schema for validation (local file sitemap.xsd)
schemas.Add("", XmlReader.Create("sitemap.xsd"));

// validating local sitemap.xml file
var doc = XDocument.Load("sitemap.xml", LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo | LoadOptions.SetBaseUri);
doc.Validate(schemas, (s, e) =>
    Console.WriteLine("Severity: {0}, Message: {1}", e.Severity, e.Message);
Send us feedback about this snippet »

Related Snippets: