Ver Mensaje Individual
  #2039  
Antiguo 26-10-2021
unomasmas unomasmas is offline
Miembro
 
Registrado: dic 2019
Posts: 112
Reputación: 5
unomasmas Va por buen camino
Cita:
Empezado por Ramon88 Ver Mensaje
Alguien sabría decirme el targetNamespace ??
Estoy intentando validar XML con el esquema XSD, y urn:ticketbai:emision no sirve, y si al pasarlo pongo nothing, no me funciona:
Código:
Dim schemas As New XmlSchemaSet()
schemas.Add(targetNamespace , pathXSD)
Creo que el problema está en la línea <import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd"/>. Parece que hay algún tipo de bloqueo en w3.org. Después de muchas vueltas, encontré la solución descargando el fichero y añadiéndolo como segundo esquema, sin más, desde local. Te dejo mi clase:

Código:
using System;
using System.Windows.Forms;
using System.Net;

using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace tBAI
{
    public class Validaciones_Formato
    {
        private bool _isValid { get; set; }
        private string _noValidInformation { get; set; }
        private string _error;

        public bool ValidaXmlFactura(string xmlFile)
        {
            try
            {
                _isValid = true;
                // Ficheros xsd ubicados en la raíz del ejecutable
                string xsdFile = @"ticketBaiV1-2.xsd";
                string xsdFile2 = @"xmldsig-core-schema.xsd";

                var path = new Uri(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath;
                XmlSchemaSet schema = new XmlSchemaSet();
                schema.Add("urn:ticketbai:emision", path + "\\" + xsdFile);
                schema.Add("http://www.w3.org/2000/09/xmldsig#", path + "\\" + xsdFile2);

                using (XmlReader rd = XmlReader.Create(xmlFile))
                {
                    XDocument doc = XDocument.Load(rd);
                    doc.Validate(schema, ValidationCallBack);
                }

                if (!_isValid)
                {
                    MessageBox.Show(_noValidInformation.TrimEnd(Environment.NewLine.ToCharArray()), System.Reflection.MethodBase.GetCurrentMethod().Name, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                }
                return _isValid;
            }
            catch (Exception ex)
            {
                //isValid = false; no need to say isValid=false as it returns false
                _error = ex.Message;
                MessageBox.Show(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                return false;
            }
        }

        public string GetNoValidInformation()
        {
            return _noValidInformation;
        }

        public string GetErrorMsg()
        {
            return _error;
        }

        private void ValidationCallBack(object sender, ValidationEventArgs e)
        {
            _noValidInformation += string.Format("+ {0}: {1}", e.Severity.ToString(), e.Message) + Environment.NewLine;
            _isValid = false;
        }
    }
}
Responder Con Cita