简体   繁体   中英

Validate XML against XSD error: This is an invalid xsi:type

I am writing a routine in C# to validate an Xml file using XmlDocument. Everything seems to be fine except something that i cant understand.

my xml:

<?xml version="1.0" encoding="utf-8"?>
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos" 
 xmlns:bdo_fosfec="http://asocajas.hp.com/bdo_fosfec" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <registro54 xsi:type="bdo_fosfec:Registro54">
  <registro82 xsi:type="bdo_fosfec:Registro82">
   ...
  </registro82>
 </registro54>
 <registro54 xsi:type="bdo_fosfec:Registro54">
  <registro82 xsi:type="bdo_fosfec:Registro82">
   ...
  </registro82>
 </registro54>
</bdo_fosfec:RegistrosPagosElement>

and the xsd:

<?xml version="1.0" encoding="UTF-8"?>
 <xsd:schema xmlns="http://asocajas.hp.com/bdo_fosfec" xmlns:tns1="http://asocajas.hp.com/bdo_fosfec" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://asocajas.hp.com/bdo_fosfec">
    <xsd:annotation>
<xsd:documentation>BOMORIGIN::platform:/resource/bdo_Fosfec/Business%20Objects/bdo_Fosfec.bom#_rs4Sa9itEeSR9aIqvSWzoQ</xsd:documentation>
   </xsd:annotation>
     ...
 </xsd:schema>

here is my code to validate my xml against xsd,

XmlDocument xml = new XmlDocument();
xml.Schemas.Add("http://asocajas.hp.com/bdo_fosfec", PathFileXSD);
//load my xml
xml.LoadXml(stringXml);

//event handler to manage the errors from XmlDocument object
ValidationEventHandler veh = new ValidationEventHandler(ErrorValidacion);

//validate my xml
xml.Validate(veh);

and the event handler ErrorValidacion will show me the error

private void ErrorValidacion(object sender, ValidationEventArgs e)
{
    string concat = string.Empty;
    switch (e.Severity)
    {
        case XmlSeverityType.Error:
             concat = string.Format("Error: {0}", e.Message);
             break;
        case XmlSeverityType.Warning:
             concat = string.Format("Warning {0}", e.Message);
             break;
    }
}

When run my program the error msj is:

This is an invalid xsi:type ' http://asocajas.hp.com/bdo_fosfec:RegistrosPagos '.

The thing is.. why this message?, The xsi:type for my xml isn't

http://asocajas.hp.com/bdo_fosfec:RegistrosPagos

The xsi:type for my xml is

xsi:type="bdo_fosfec:RegistrosPagos"

Where does the xsi:type http://asocajas.hp.com/bdo_fosfec:RegistrosPagos come from?

So, How can I solve this issue without having to modify the xml? (Because the xml is based on an xslt)

The error message is saying that the XSD does not include a declaration for RegistrosPagos in the http://asocajas.hp.com/bdo_fosfec namespace.

It is using the expanded form of the namespace to report the error instead of the namespace prefix because the namespace prefix is arbitrary and insignificant by definition. This is entirely proper.

In fact, the XSD you've posted does not include a RegistrosPagos in the http://asocajas.hp.com/bdo_fosfec namespace.

See also: How to restrict the value of an XML element using xsi:type in XSD?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM