How to validate, on run time, xml against xsd without save the file on local folder?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jimis
    New Member
    • Jun 2010
    • 2

    How to validate, on run time, xml against xsd without save the file on local folder?

    I need to validate XMLs files against XSDs. The XML will be downloaded from URL and I will keep it as StremReader or XmlDocument. The XSD will return from DataBase as nvarchar(max). I am prohibited to save the files locally. Has anybody an example how to deal with this situation? I am trying this way but I am getting the XmlException "Root element is missing".


    public void voltaXsd_em_Str eamReader()
    {
    //strArquivoIntei ro will contain the XSD comming from database as nvarchar(max) but I make simpler here.
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(@"C :\file.xsd");
    string strArquivoIntei ro = xmlDoc.OuterXml ;

    byte[] byteArray = Encoding.ASCII. GetBytes(strArq uivoInteiro);
    MemoryStream streamXSD = new MemoryStream(by teArray);

    StreamReader readerXsd = new StreamReader(st reamXSD);

    XmlReaderSettin gs settings = new XmlReaderSettin gs();
    settings.Valida tionEventHandle r += this.Validation EventHandler;

    settings.Valida tionType = ValidationType. Schema;
    settings.Schema s.Add("schema.x sd", XmlReader.Creat e(readerXsd));
    settings.CheckC haracters = true;

    XmlReader XmlValidatingRe ader = XmlReader.Creat e(@"C:\file.xml ", settings);

    XmlTextReader Reader = new XmlTextReader(@ "file.xml") ;

    XmlSchema Schema = new XmlSchema();

    //Exactlly here I am getting the exception "Root element is missing" and I do not know why.

    Schema = XmlSchema.Read( readerXsd, ValidationEvent Handler);

    XmlValidatingRe ader ValidatingReade r = new XmlValidatingRe ader(Reader);

    //// 6- Setar o tipo de validação para o objeto XmlValidationRe ader
    ValidatingReade r.ValidationTyp e = ValidationType. Schema;

    //// 7- Adicionar Schema a coleção de Schemas XmlValidationRe ader
    ValidatingReade r.Schemas.Add(S chema);

    try
    {
    //---------------

    XmlValidatingRe ader.Read();
    XmlValidatingRe ader.Close();

    //// 8- Adicionar o endereço do ValidationEvent Handler ao ValidationEvent Handler do XmlValidationRe ader
    ValidatingReade r.ValidationEve ntHandler += ValidationEvent Handler;


    //9- Validar cada nó
    while ((ValidatingRea der.Read()))
    {

    }


    ValidatingReade r.Close();
    }
    catch (Exception ex)
    {
    ValidatingReade r.Close();
    XmlValidatingRe ader.Close();

    }
    }
    private void ValidationEvent Handler(object sender, ValidationEvent Args args)
    {
    bool blnXmlValido;
    if (args.Severity == XmlSeverityType .Warning)
    {

    blnXmlValido = false;
    }
    else if (args.Severity == XmlSeverityType .Error)
    {

    blnXmlValido = false;
    }
    else if (!(string.IsNul lOrEmpty(args.E xception.ToStri ng())))
    {

    blnXmlValido = false;
    }

    if ((args.Exceptio n != null))
    {

    }
    }
Working...