【问题标题】:Validate XML against XSD error: This is an invalid xsi:type针对 XSD 错误验证 XML:这是无效的 xsi:type
【发布时间】:2017-07-19 16:29:46
【问题描述】:

我正在用 C# 编写一个例程来使用 XmlDocument 验证 Xml 文件。一切似乎都很好,除了我无法理解的东西。

我的 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>

还有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>

这是我针对 xsd 验证我的 xml 的代码,

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);

事件处理程序 ErrorValidacion 将向我显示错误

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;
    }
}

运行我的程序时,错误 msj 是:

这是一个无效的 xsi:type 'http://asocajas.hp.com/bdo_fosfec:RegistrosPagos'。

问题是.. 为什么这条消息?,我的 xml 的 xsi:type 不是

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

我的 xml 的 xsi:type 是

xsi:type="bdo_fosfec:RegistrosPagos"

xsi:type http://asocajas.hp.com/bdo_fosfec:RegistrosPagos 来自哪里?

那么,我怎样才能在不修改 xml 的情况下解决这个问题呢? (因为xml是基于一个xslt)

【问题讨论】:

    标签: c# xml xsd


    【解决方案1】:

    错误消息表明 XSD 未在 http://asocajas.hp.com/bdo_fosfec 命名空间中包含 RegistrosPagos 的声明。

    它使用命名空间的扩展形式而不是命名空间前缀来报告错误,因为命名空间前缀在定义上是任意且无关紧要的。这是完全正确的。

    事实上,您发布的 XSD 并没有在 http://asocajas.hp.com/bdo_fosfec 命名空间中包含 RegistrosPagos

    另请参阅:How to restrict the value of an XML element using xsi:type in XSD?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      相关资源
      最近更新 更多