2017-07-19 26 views
0

我正在C#中編寫一個例程來驗證使用XmlDocument的Xml文件。除了我無法理解的事情之外,一切似乎都很好。根據XSD錯誤驗證XML:這是一個無效的xsi:類型

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

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

的事情是..爲什麼這個消息的?XSI:類型我的XML是不是

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

的XSI:鍵入我的xml是

xsi:type="bdo_fosfec:RegistrosPagos"

xsi:type http://asocajas.hp.com/bdo_fosfec:RegistrosPagos從哪裏來?

那麼,我怎樣才能解決這個問題,而無需修改XML? (因爲xml基於xslt)

回答

0

錯誤消息是說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?