You must Sign In to post a response.
  • Category: XML

    Validate XML with XSD using ASP.net

    Hi Everybody,

    I have one requirement, I need to validate XML with XSD. I tried that but the thing is in XML I have a nodes in tree structure but in XSD elements are in root itself.

    For EX:
    In XML
    A--> B ( is child of A)--- >C ( Child of B)

    but in XSD
    A --- root
    B --- root
    C --- root

    Any one give me some suggestion to resolve this issue.

    Thanks in Advance.

    Regards,
    Naveen
  • #743713
    Hi Naveen,
    you need to validate means you need to check the xml format against xsd right ?
    I have a code where I can validate the xml against xsd.
    see below snippet

    //call following Boolean function
    private bool validateSchema(String szFileName)
    {
    //this function will validate the schema file (xsd)

    XmlSchema myschema;
    m_Success = true; //'make sure to reset the success var
    StreamReader sr = new StreamReader(szFileName);
    try
    {
    //sr = new StreamReader(szFileName);
    myschema = XmlSchema.Read(sr, new ValidationEventHandler (ValidationCallBack));
    //'This compile statement is what ususally catches the errors
    myschema.Compile(new ValidationEventHandler (ValidationCallBack));
    }
    finally
    {
    sr.Close();
    }
    return m_Success;
    }

    private void ValidationCallBack(Object sender, ValidationEventArgs args)
    {
    //'Display the validation error. This is only called on error
    m_Success = false; //'Validation failed
    writertbox("Validation error: " + args.Message);
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #743715
    Hi Prasad,

    Can you please conform one thing in above code "szFileName" means XML file name or XSD file name. And please confirm that this code will work for my above requirement. i.e. xml child node matches with xsd root node. Please confirm that one also.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments