none
XML validation using xslt from c# RRS feed

  • Question

  • Hi,

    I have to validate XML file against XSLT file can anyone how can I do that.

    I have schematron file also if someone know how to validate from that.  




    Thursday, August 22, 2013 4:53 PM

Answers

  • Hello Shriniwas,

    Welcome to MSDN forum.

    According to your description, you have a trouble with validating XML file.

    >>I have to validate XML file against XSLT file can anyone how can I do that.

    XML Stylesheet Transformations (XSLT) is defined as a language for transforming source XML documents into another document formats using XML Path Language (XPath) as a query language. This is particularly useful when incoming or outgoing XML documents do not match the document type expected by the target system.

    I give you a sample here.

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Xsl;
    using System.Xml.XPath;
    public class TransformXML
    {
        //This will transform xml document using xslt and produce result xml document
        //and display it
        public static void Main(string[] args)
        {
            try
            {
                XPathDocument myXPathDocument = new XPathDocument(sourceDoc);
                XslTransform myXslTransform = new XslTransform();
                XmlTextWriter writer = new XmlTextWriter(resultDoc, null);
                myXslTransform.Load(xsltDoc);
                myXslTransform.Transform(myXPathDocument, null, writer);
                writer.Close();
                StreamReader stream = new StreamReader (resultDoc);
                Console.Write("**This is result document**\n\n");
                Console.Write(stream.ReadToEnd());
            }
            catch (Exception e)
            {
                Console.WriteLine ("Exception: {0}", e.ToString());
            }
        }
    }

    If you have any question, please feel free to let me know.
    Have a nice day!

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 23, 2013 11:14 AM
  • Hello Shriniwas,

    >>I have schematron file also if someone know how to validate from that. 

    You can use the XmlValidatingReader to validate XML documents against XML Schema definition language (XSD) schemas from multiple schemas.

    You can have a look at the reference below.
    http://msdn.microsoft.com/en-us/library/wc8csdkz.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    To save you some time, I give you the sample code here.

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    namespace ValidationSample
    {
       class Sample
       {
          public static void Main()
          {
             FileStream stream = new FileStream("Mixed.xml", FileMode.Open);
             XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);
             vr.Schemas.Add(null, "Book.xsd");
             vr.Schemas.Add(null, "Tape.xsd");
             vr.ValidationType = ValidationType.Schema;
             vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);
             while(vr.Read());
             Console.WriteLine("Validation finished");
          }
          public static void ValidationHandler(object sender, ValidationEventArgs args)
          {
             Console.WriteLine("***Validation error");
             Console.WriteLine("\tSeverity:{0}", args.Severity);
             Console.WriteLine("\tMessage:{0}", args.Message);
          }
       }
    }

    If you have any question, please feel free to let me know.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, August 23, 2013 11:15 AM

All replies

  • Hello Shriniwas,

    Welcome to MSDN forum.

    According to your description, you have a trouble with validating XML file.

    >>I have to validate XML file against XSLT file can anyone how can I do that.

    XML Stylesheet Transformations (XSLT) is defined as a language for transforming source XML documents into another document formats using XML Path Language (XPath) as a query language. This is particularly useful when incoming or outgoing XML documents do not match the document type expected by the target system.

    I give you a sample here.

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Xsl;
    using System.Xml.XPath;
    public class TransformXML
    {
        //This will transform xml document using xslt and produce result xml document
        //and display it
        public static void Main(string[] args)
        {
            try
            {
                XPathDocument myXPathDocument = new XPathDocument(sourceDoc);
                XslTransform myXslTransform = new XslTransform();
                XmlTextWriter writer = new XmlTextWriter(resultDoc, null);
                myXslTransform.Load(xsltDoc);
                myXslTransform.Transform(myXPathDocument, null, writer);
                writer.Close();
                StreamReader stream = new StreamReader (resultDoc);
                Console.Write("**This is result document**\n\n");
                Console.Write(stream.ReadToEnd());
            }
            catch (Exception e)
            {
                Console.WriteLine ("Exception: {0}", e.ToString());
            }
        }
    }

    If you have any question, please feel free to let me know.
    Have a nice day!

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 23, 2013 11:14 AM
  • Hello Shriniwas,

    >>I have schematron file also if someone know how to validate from that. 

    You can use the XmlValidatingReader to validate XML documents against XML Schema definition language (XSD) schemas from multiple schemas.

    You can have a look at the reference below.
    http://msdn.microsoft.com/en-us/library/wc8csdkz.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    To save you some time, I give you the sample code here.

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    namespace ValidationSample
    {
       class Sample
       {
          public static void Main()
          {
             FileStream stream = new FileStream("Mixed.xml", FileMode.Open);
             XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);
             vr.Schemas.Add(null, "Book.xsd");
             vr.Schemas.Add(null, "Tape.xsd");
             vr.ValidationType = ValidationType.Schema;
             vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);
             while(vr.Read());
             Console.WriteLine("Validation finished");
          }
          public static void ValidationHandler(object sender, ValidationEventArgs args)
          {
             Console.WriteLine("***Validation error");
             Console.WriteLine("\tSeverity:{0}", args.Severity);
             Console.WriteLine("\tMessage:{0}", args.Message);
          }
       }
    }

    If you have any question, please feel free to let me know.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, August 23, 2013 11:15 AM
  • Sorry still the objective of xslt is not clear. so show me what is the usage of xslt in c# programming when working with xml file.
    Friday, November 18, 2016 1:30 PM