I wrote a helper utility to work with XMl files in C#, these are some common set of functions that i have been using for day-to-day work, I compiled it inside a common entity now ;)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Xml; | |
using System.Xml.Linq; | |
using System.Xml.Schema; | |
using System.Xml.Xsl; | |
namespace Killercodes.Xml | |
{ | |
public static class XmlUtilities | |
{ | |
public static string InnerXML (XElement xElement) | |
{ | |
var reader = xElement.CreateReader(); | |
reader.MoveToContent(); | |
return reader.ReadInnerXml(); | |
} | |
public static string OuterXML (XElement xElement) | |
{ | |
var reader = xElement.CreateReader(); | |
reader.MoveToContent(); | |
return reader.ReadOuterXml(); | |
} | |
private static XElement RemoveAllNamespaces (XElement xElement) | |
{ | |
if (!xElement.HasElements) | |
{ | |
XElement newXElement = new XElement(xElement.Name.LocalName); | |
newXElement.Value = xElement.Value; | |
foreach (XAttribute attribute in xElement.Attributes()) | |
newXElement.Add(attribute); | |
return newXElement; | |
} | |
return new XElement(xElement.Name.LocalName, xElement.Elements().Select(el => RemoveAllNamespaces(el))); | |
} | |
public static string ApplyTransformation (string xmlContent, string xsltFilePath) | |
{ | |
XslCompiledTransform transform = new XslCompiledTransform(true); | |
transform.Load(xsltFilePath, XsltSettings.TrustedXslt, new XmlUrlResolver()); | |
XElement xmlDocumentWithoutNs = null; | |
if (xmlContent.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse((File.ReadAllText(xmlContent)))); | |
} | |
else | |
{ | |
xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlContent)); | |
} | |
XmlReader reader = XmlReader.Create(new StringReader(xmlDocumentWithoutNs.ToString())); | |
StringWriter output = new StringWriter(); | |
XmlWriter writer = XmlWriter.Create(output, transform.OutputSettings); | |
transform.Transform(reader, writer); | |
return output.ToString(); | |
} | |
public static void SchemaValidation (string xmlFilePath) | |
{ | |
// Set the validation settings. | |
XmlReaderSettings settings = new XmlReaderSettings(); | |
settings.ValidationType = ValidationType.Schema; | |
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; | |
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; | |
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; | |
settings.ValidationEventHandler += new ValidationEventHandler(SchemaValidationCallBack); | |
// Create the XmlReader object. | |
XmlReader reader = XmlReader.Create(xmlFilePath, settings); | |
// Parse the file. | |
while (reader.Read()) ; | |
} | |
// Display any warnings or errors. | |
private static void SchemaValidationCallBack (object sender, ValidationEventArgs args) | |
{ | |
if (args.Severity == XmlSeverityType.Warning) | |
Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message); | |
else | |
Console.WriteLine("\tValidation error: " + args.Message); | |
} | |
public static void SchemaValidation (string xmlFilePath, string schemaFilePath) | |
{ | |
// Create the XmlSchemaSet class. | |
XmlSchemaSet sc = new XmlSchemaSet(); | |
sc.Add(null, schemaFilePath); | |
// Set the validation settings. | |
XmlReaderSettings settings = new XmlReaderSettings(); | |
settings.ValidationType = ValidationType.Schema; | |
settings.Schemas = sc; | |
settings.ValidationEventHandler += new ValidationEventHandler(SchemaValidationCallBack); | |
// Create the XmlReader object. | |
XmlReader reader = XmlReader.Create(xmlFilePath, settings); | |
// Parse the file. | |
while (reader.Read()) ; | |
//Close the reader. | |
reader.Close(); | |
} | |
public static bool ValidateSchema (string xmlPath, string xsdPath) | |
{ | |
XmlDocument xml = new XmlDocument(); | |
xml.Load(xmlPath); | |
xml.Schemas.Add(null, xsdPath); | |
try | |
{ | |
xml.Validate(null); | |
} | |
catch (XmlSchemaValidationException) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} | |
} |