.NET,XmlSerializer InvalidOperationException,由于 XmlSchema 定义?

发布于 2024-09-12 06:31:45 字数 2491 浏览 1 评论 0原文

我已将包含我要读取的 XML 文件和相应 XSD 文件的 ZIP 文件上传到 http://www.bonnland.de/FIBEX.zip

我正在尝试使用 XmlSerializer 反序列化以下 XML(片段)。这样做时,我收到错误:(抱歉,它是德语,我将用斜体进行粗略翻译)

System.InvalidOperationException==>Fehler im XML-Dokument (90,7).
System.InvalidOperationException==>Der angegebene Typ wurde nicht erkannt:  Name='CONTROLLER-TYPE', Namespace='http://www.asam.net/xml/fbx/can', bei .

这翻译为:

System.InvalidOperationException==>error in XML document (90,7).
System.InvalidOperationException==>the given type could not be found: Name='CONTROLLER-TYPE', Namespace='http://www.asam.net/xml/fbx/can', at 

这是源文档:

<fx:ECU ID="ecuSpeedControl">
  <ho:SHORT-NAME>SpeedControl</ho:SHORT-NAME> 
  <ho:DESC>ECU controlling drive speed</ho:DESC> 
  <fx:CONTROLLERS>
  <fx:CONTROLLER xsi:type="can:CONTROLLER-TYPE" ID="ctrlSpeedControl">
  <ho:SHORT-NAME>ctrlSpeedControl</ho:SHORT-NAME> 
  <ho:DESC>CAN controller of ECU</ho:DESC> 
  <fx:CHIP-NAME>SJA1000</fx:CHIP-NAME> 
  <can:TIME-SEG0>11</can:TIME-SEG0> 
  <can:TIME-SEG1>4</can:TIME-SEG1> 
  <can:SYNC-JUMP-WIDTH>2</can:SYNC-JUMP-WIDTH> 
  <can:NUMBER-OF-SAMPLES>1</can:NUMBER-OF-SAMPLES> 
  </fx:CONTROLLER>
  </fx:CONTROLLERS>
  </fx:ECU>

根元素是:

<fx:FIBEX xmlns:fx="http://www.asam.net/xml/fbx" xmlns:ho="http://www.asam.net/xml"     
    xmlns:can="http://www.asam.net/xml/fbx/can" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="fibex4can.xsd" VERSION="3.1.0">

类定义对于这个片段是:

public ref class FIBEXECU : AbstractFIBEXNode, IGenericContainable
 {
 public:

  ref class ControllersContainer : FIBEXGenericContainer<FIBEXController^>{
  public:
   [XmlElement("CONTROLLER")]
   property array<FIBEXController^>^ ControllerObjs {
    array<FIBEXController^>^ get() { return Children;}
    void set(array<FIBEXController^>^ value) { Children = value;}
   }
  };

  [XmlAttribute("ID")]
  virtual property String^ ID;

  [XmlElement("SHORT-NAME", Namespace="http://www.asam.net/xml")]
  property String^ ShortName;

  [XmlElement("CONTROLLERS")]
  property ControllersContainer^ Controllers;
};

我希望(再次)有人可以帮助我,因为我没有在谷歌或这里找到解决方案。

I've uploaded a ZIP file containing both the XML file I'm trying to read and the corresponding XSD files to http://www.bonnland.de/FIBEX.zip

I'm trying to deserialize the following XML (fragment) using XmlSerializer. While doing so I get the error: (Sorry for it being German I'll give a rough translation in italics)

System.InvalidOperationException==>Fehler im XML-Dokument (90,7).
System.InvalidOperationException==>Der angegebene Typ wurde nicht erkannt:  Name='CONTROLLER-TYPE', Namespace='http://www.asam.net/xml/fbx/can', bei .

This translates as something like:

System.InvalidOperationException==>error in XML document (90,7).
System.InvalidOperationException==>the given type could not be found: Name='CONTROLLER-TYPE', Namespace='http://www.asam.net/xml/fbx/can', at 

Here's the source document:

<fx:ECU ID="ecuSpeedControl">
  <ho:SHORT-NAME>SpeedControl</ho:SHORT-NAME> 
  <ho:DESC>ECU controlling drive speed</ho:DESC> 
  <fx:CONTROLLERS>
  <fx:CONTROLLER xsi:type="can:CONTROLLER-TYPE" ID="ctrlSpeedControl">
  <ho:SHORT-NAME>ctrlSpeedControl</ho:SHORT-NAME> 
  <ho:DESC>CAN controller of ECU</ho:DESC> 
  <fx:CHIP-NAME>SJA1000</fx:CHIP-NAME> 
  <can:TIME-SEG0>11</can:TIME-SEG0> 
  <can:TIME-SEG1>4</can:TIME-SEG1> 
  <can:SYNC-JUMP-WIDTH>2</can:SYNC-JUMP-WIDTH> 
  <can:NUMBER-OF-SAMPLES>1</can:NUMBER-OF-SAMPLES> 
  </fx:CONTROLLER>
  </fx:CONTROLLERS>
  </fx:ECU>

The root element is:

<fx:FIBEX xmlns:fx="http://www.asam.net/xml/fbx" xmlns:ho="http://www.asam.net/xml"     
    xmlns:can="http://www.asam.net/xml/fbx/can" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="fibex4can.xsd" VERSION="3.1.0">

the class definition for this fragment is:

public ref class FIBEXECU : AbstractFIBEXNode, IGenericContainable
 {
 public:

  ref class ControllersContainer : FIBEXGenericContainer<FIBEXController^>{
  public:
   [XmlElement("CONTROLLER")]
   property array<FIBEXController^>^ ControllerObjs {
    array<FIBEXController^>^ get() { return Children;}
    void set(array<FIBEXController^>^ value) { Children = value;}
   }
  };

  [XmlAttribute("ID")]
  virtual property String^ ID;

  [XmlElement("SHORT-NAME", Namespace="http://www.asam.net/xml")]
  property String^ ShortName;

  [XmlElement("CONTROLLERS")]
  property ControllersContainer^ Controllers;
};

I hope that (yet again) someone can help me, as I didn't find a solution on google or here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-09-19 06:31:45

您收到的错误似乎表明某种类型不可用。查看您的 XSD,有相当多的类型未定义,但这可能是因为您没有包含导入和包含的 XSD 文件,因此我无法可靠地检查您的文档的有效性。

XML 本身包含错误。例如,xsi:schemaLocation 不正确,它必须包含命名空间和位置对。而不是这个:

xsi:schemaLocation="fibex4can.xsd" 

它应该是这个(假设该文件确实与 XML 位于同一目录中):

xsi:schemaLocation="http://www.asam.net/xml/fbx/can fibex4can.xsd" 

我的猜测是,文档的明显错误是无法解析它的原因。基本上,在处理 XML 时,您必须非常严格(与任何编程语言一样)。如果您告诉处理器验证文档,那么架构必须可用,它们本身必须有效,任何相关架构必须可定位,最后,XML 文档必须针对这些架构有效。符合标准的处理器(例如使用 .NET 的处理器)必须遵守这些和其他 XML 规则,并且当 XML 格式不正确或无效时,它们必须抛出错误并停止解析文档。

The error you get seems to indicate that a certain type is not available. Looking through your XSD, there are quite some types undefined, but that's likely because you haven't included the imported and included XSD files, so I cannot reliably check the validity of your documents.

The XML itself contains errors. For instance, the xsi:schemaLocation is not correct, it must contain pairs with the namespace and the location. Instead of this:

xsi:schemaLocation="fibex4can.xsd" 

it should be this (assuming the file is indeed in the same directory as the XML):

xsi:schemaLocation="http://www.asam.net/xml/fbx/can fibex4can.xsd" 

My guess is, that the apparent errors of your document are the reasons that it cannot be parsed. Basically, when dealing with XML, you must be very strict (as with any programming language). If you tell the processor to validate the document, then the Schema's must be available, they must themselves be valid, any related schema's must be locatable and finally, the XML document must be valid against these schema's. Conforming processors (like the ones with .NET) must obey these and other rules for XML and they must throw an error and stop parsing the document when the XML is not well-formed or not valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文