views:

38

answers:

1

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_StreamReader()
{
     // strArquivoInteiro 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 strArquivoInteiro = xmlDoc.OuterXml;

     byte[] byteArray = Encoding.ASCII.GetBytes(strArquivoInteiro);
     MemoryStream streamXSD = new MemoryStream(byteArray);
     StreamReader readerXsd = new StreamReader(streamXSD);

     XmlReaderSettings settings = new XmlReaderSettings();
     settings.ValidationEventHandler += this.ValidationEventHandler;

     settings.ValidationType = ValidationType.Schema;
     settings.Schemas.Add("schema.xsd", XmlReader.Create(readerXsd));
     settings.CheckCharacters = true;

     XmlReader XmlValidatingReader = XmlReader.Create(@"C:\file.xml", settings);

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

     XmlSchema Schema = new XmlSchema();

     // Exactly here I am getting the exception 
     // "Root element is missing" and I do not know why. 
     Schema = XmlSchema.Read(readerXsd, ValidationEventHandler);

     XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader);

     //// 6- Setar o tipo de validação para o objeto XmlValidationReader 
     ValidatingReader.ValidationType = ValidationType.Schema;

     //// 7- Adicionar Schema a coleção de Schemas XmlValidationReader
     ValidatingReader.Schemas.Add(Schema);

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

            XmlValidatingReader.Read();
            XmlValidatingReader.Close();

            //// 8- Adicionar o endereço do ValidationEventHandler ao ValidationEventHandler do XmlValidationReader
            ValidatingReader.ValidationEventHandler += ValidationEventHandler;

            //9- Validar cada nó
            while ((ValidatingReader.Read()))
            {
            }

            ValidatingReader.Close();
        }
        catch (Exception ex)
        {
            ValidatingReader.Close();
            XmlValidatingReader.Close();
        }
    }

    private void ValidationEventHandler(object sender, ValidationEventArgs args)
    {
        bool blnXmlValido;

        if (args.Severity == XmlSeverityType.Warning)
        {
            blnXmlValido = false;
        }
        else if (args.Severity == XmlSeverityType.Error)
        {
            blnXmlValido = false;
        }
        else if (!(string.IsNullOrEmpty(args.Exception.ToString())))
        {
            blnXmlValido = false;
        }

        if ((args.Exception != null))
        {
        }
    }
+1  A: 

This is a little hard to debug without seeing an example of the xml you are sending in. Have you verified that the xml document being read in contains a root element tag that encloses all other tags?

This error occurs when your xml is missing the root tag as well as if the xml document is blank.

Also verify that your stream operations are set to the beginning of the stream.

gwhitake