【问题标题】:XmlSerializer chokes on xsi:type?xsi:type 上的 XmlSerializer 阻塞?
【发布时间】:2009-09-29 21:30:44
【问题描述】:

编辑:请参阅下面 Pavel 的回答——“类型无法识别错误”是由我的解析代码(我没有在此处包含)中的类层次结构与我的 XML 模式之间的不匹配引起的。 “类型未识别”错误有点像红鲱鱼,但一旦我修复了问题,解析就可以正常工作。如果您是因为在 Google 上搜索了该错误消息而遇到此问题,请仔细检查您的层次结构!

我有如下所示的 XML 文件。在 VS2008 中查看时,Intellisense 不报任何错误。但是,当我尝试使用 XmlSerializer.Deserialize 解析它时,我得到了异常:

指定的类型未被识别: 名称='UTCTimeStamp_t', 命名空间='http://www.fixprotocol.org/ATDL-1-1/Core', 在 http://www.fixprotocol.org/ATDL-1-1/Core'>.

(有问题的 xsi:Type,“UTCTimeStamp_t”,在引用的 XSD 文件中定义,如下面的 XML 所示。)

为什么 XmlSerializer 拒绝 XML,即使 Intellisense 显示它是有效的? (我仔细检查了两者是否都获得了 XSD 文件的相同副本。)

我可以进行更改或应用解决方法来解析它吗?

注意:我在 Silverlight3 中执行此操作——该版本的序列化程序是否有任何特殊限制?

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<Strategies
 xmlns="http://www.fixprotocol.org/ATDL-1-1/Core"
  xmlns:core="http://www.fixprotocol.org/ATDL-1-1/Core"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xsi:schemaLocation="http://www.fixprotocol.org/ATDL-1-1/Core http://www.myserver.com/atdl-core-1-1.xsd"
  strategyIdentifierTag="7620"
  >

  <Strategy name="Tazer1" uiRep="Tazer" wireValue="Tazer" version="1" fixMsgType="D" providerID="ABC">

    <Parameter name="StartTime" xsi:type="core:UTCTimeStamp_t" fixTag="7602" use="required" localMktTz="America/New_York">
      <Description>Strategy Start Time</Description>
    </Parameter>
  </Strategy>

</Strategies>

这是 XSD 文件的 sn-p:

  <xs:complexType name="UTCTimeStamp_t">
    <xs:complexContent>
      <xs:extension base="core:Parameter_t">
        <xs:attribute name="minValue" type="core:UTCTimeStamp"/>
        <xs:attribute name="maxValue" type="core:UTCTimeStamp"/>
        <xs:attribute name="dailyMinValue" type="core:LocalMktTime"/>
        <xs:attribute name="dailyMaxValue" type="core:LocalMktTime"/>
        <xs:attribute name="localMktTz" type="xs:string">
          <xs:annotation>
            <xs:documentation>
              These string values must correspond to time zone values from the zoneinfo database,
              also known as the tz database.
            </xs:documentation>
          </xs:annotation>
        </xs:attribute>
        <xs:attribute name="constValue" type="core:UTCTimeStamp"/>
        <xs:attribute name="dailyConstValue" type="core:LocalMktTime"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

【问题讨论】:

  • 请发布更多您的 XSD。你在哪里定义&lt;Strategy&gt;

标签: .net silverlight xml-serialization


【解决方案1】:

XmlSerializer 不关心类型的 XML Schema 定义——它只关心类型的名称以及映射到它的类声明。如果不查看您尝试反序列化的类的定义,以及它们的 XML 序列化属性,就不可能说出问题所在。

【讨论】:

  • 谢谢,帕维尔。知道 XmlSerializer 不查看架构定义是我解决问题所需的关键。事实证明,我的类层次结构和我的模式之间存在不匹配,因此“类型”属性被一个与我想象的完全不同的类解析。哦!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-20
  • 2018-05-01
相关资源
最近更新 更多