locked
Validate XML string agains XSD. RRS feed

  • Question

  • User-1293249277 posted

    I want to validate my XML with an XSD. I have already written below code. But now my problem is that i want to validate XML String and not XML file which is already stored in the solution. So insterad if the person.xml. I want to validate the string person = "some valid xml";

    public void ValidateXMLXSD()
    {
    string sFileName = Server.MapPath("person.xml");
    XmlTextReader oTr = new XmlTextReader(sFileName);
    XmlValidatingReader oVr = new XmlValidatingReader(oTr);

    oVr.ValidationType = ValidationType.Schema;

    oVr.ValidationEventHandler += new ValidationEventHandler(this.ValidationEvent);

    while (oVr.Read())
    {
    if (oVr.NodeType == XmlNodeType.Text)
    {
    // lstXml.Items.Add(oVr.Value);
    }
    }

    }

    private void ValidationEvent(object s, ValidationEventArgs args)
    {
    Response.Write(args.Message.ToString());
    //THe error messages will be print on screen.
    }

    Tuesday, February 19, 2013 6:41 AM

Answers

  • User1938476581 posted

    Hi,

    Please refer this

                                 XmlValidatingReader reader  = null;
                                 XmlSchemaCollection myschema = new XmlSchemaCollection();
                                 ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors );
                                  try
    			{
    				//Create the XML fragment to be parsed.
    				String xmlFrag = "<author  xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
    					"<first-name>Herman</first-name>" +
    					"<last-name>Melville</last-name>" +
    					"</author>";
    
    				//Create the XmlParserContext.
    				XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
                  
    				//Implement the reader. 
    				reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
    				//Add the schema.
    				myschema.Add("urn:bookstore-schema", "c:\\Books.xsd");
    
    				//Set the schema type and add the schema to the reader.
    				reader.ValidationType = ValidationType.Schema;
    				reader.Schemas.Add(myschema);
    
    				while (reader.Read())
    				{
    				}
                   
    				Console.WriteLine("Completed validating xmlfragment");
    			}
    			catch (XmlException XmlExp)
    			{
    				Console.WriteLine(XmlExp.Message);
    			}
    			catch(XmlSchemaException XmlSchExp)
    			{
    				Console.WriteLine(XmlSchExp.Message);
    			}
    			catch(Exception GenExp)
    			{
    				Console.WriteLine(GenExp.Message);
    			}
    			finally
    			{
    				Console.Read();
    			}
    

    For detailed information, you can refer here

    http://support.microsoft.com/kb/318504

    http://www.codeproject.com/Articles/8781/C-XML-Schema-Validator

    http://www.dreamincode.net/forums/topic/49826-validate-a-xml-string-against-xsd/

    Hope it can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 20, 2013 12:08 AM