XmlSerializer在属性中有命名空间前缀时抛出InvalidOperationException异常

3

我尝试读取一个包含以下元素的XML文件:

<ho:CODED-TYPE ho:BASE-DATA-TYPE="A_UINT16" CATEGORY="STANDARD-LENGTH-TYPE" ENCODING="UNSIGNED">

我用以下代码来描述这个节点:

public ref class FIBEXCodedType 
 {
 public:
  [XmlAttribute("ho:BASE-DATA-TYPE")]
  property String^ BaseDataType;

  [XmlAttribute("CATEGORY")]
  property String^ Category;

  [XmlAttribute("ENCODING")]
  property String^ Encoding;

  FIBEXCodedType(void);
 };

我从XmlSerializer.ctor中得到了一个InvalidOperationException异常,告诉我:

"Ungültiges Namenszeichen in 'ho:BASE-DATA-TYPE'."(这可以被翻译为“'ho:BASE-DATA-TYPE'中有无效字符”)。

我还尝试了以下方法:

[XmlAttribute("BASE-DATA-TYPE", Namespace="http://www.asam.net/xml")]
property String^ BaseDataType;

但是这也没有起作用。这一次没有错误消息,但单元测试失败告诉我,该属性仍然设置为“null”。
我完全被卡住了,所以任何帮助都将不胜感激。
提前致谢。
编辑:更多XML
<?xml version="1.0" ?>
<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">

<fx:CODING ID="codingSpeed">
    <ho:SHORT-NAME>CodingSpeed</ho:SHORT-NAME>
    <ho:DESC>Coding for speed values within this system.</ho:DESC>
    <ho:CODED-TYPE ho:BASE-DATA-TYPE="A_UINT16" CATEGORY="STANDARD-LENGTH-TYPE" ENCODING="UNSIGNED">
    <ho:BIT-LENGTH>16</ho:BIT-LENGTH>
    </ho:CODED-TYPE>
</fx:CODING>

你一次性在不同的马上下注:http://social.msdn.microsoft.com/Forums/en/asmxandxml/thread/b07be6c7-6a86-4a2f-be72-64972ff0b1ff ;-) - Abel
是的,我知道 :-( 很难在那里得到答案,我的单元测试栏仍然是红色的,所以我感到非常害怕 :-( - yas4891
1个回答

3

在OP编辑后重新撰写了整个答案

我最初对错误的理解是错误的。出现错误时,序列化程序初始化,而不是在读取XML时。在名称中不能使用冒号。如果指定命名空间,请不要指定前缀。实际上,你很少指定前缀(它只是命名空间的占位符)。

这样做后,你已经注意到值变成了null。原因是序列化器默认为无限定属性。如果你有限定属性,它会假设属性命名空间与元素命名空间不同。这将起作用:

<!-- this works (if namespaces are indeed different -->
<ho:CODED-TYPE fx:BASE=DATA-TYPE="A_UINT16"...>

<!-- this works, unqualified name takes namespace of parent element -->
<ho:CODED-TYPE BASE=DATA-TYPE="A_UINT16"...>

<!-- this fails, because XmlSerializer does not expect qualified attributes -->
<ho:CODED-TYPE ho:BASE=DATA-TYPE="A_UINT16"...>

这个问题看起来很奇怪。这里有一个与之类似的MSDN报告(链接),帮助我找到了解决方法。只需将属性标记为合格即可。下面是使用您的输入XML的解决方案(请注意:XmlSchemaForm.Qualified):
[XmlRoot(ElementName = "FIBEX", Namespace = "http://www.asam.net/xml/fbx")]
public class FIBEX
{
    [XmlElement("CODING", Namespace = "http://www.asam.net/xml/fbx")]
    public FIBEXCoding Coding { get; set; }
}

public class FIBEXCoding
{
    [XmlElement("SHORT-NAME", Namespace = "http://www.asam.net/xml")]
    public string ShortName { get; set; }

    [XmlElement("DESC", Namespace = "http://www.asam.net/xml")]
    public string ShortDescription { get; set; }

    [XmlElement("CODED-TYPE", Namespace = "http://www.asam.net/xml")]
    public FIBEXCodedType Codedtype { get; set; }
}

public class FIBEXCodedType
{

    [XmlAttribute("BASE-DATA-TYPE", 
        Namespace = "http://www.asam.net/xml",
        Form=XmlSchemaForm.Qualified)]
    public string BaseDataType { get; set; }

    [XmlAttribute("CATEGORY")]
    public string Category { get; set; }

    [XmlAttribute("ENCODING")]
    public string Encoding { get; set; }

    [XmlElement("BIT-LENGTH", Namespace = "http://www.asam.net/xml")]
    public int BitLength { get; set; }
}

Wooohaaa。那篇帖子中有一个错误 :-(请稍等一下。 - yas4891
@yas4891:请展示你的输入XML的确切声明,包括命名空间是如何声明的。此外,请展示一个最小化的输入样本,以便说明失败的情况。你使用模式验证器吗?还是涉及到DTD? - Abel
你真的想让我发布完整的XmlSource吗?那大约有7KB大小。 我们是否可以就有问题的根元素+父元素达成一致?我的序列化到目前为止对于文档的其余部分都有效,但对于这个位于“错误”命名空间中的属性却无法正常工作。我设法让另一个属性(“xml:lang”)按照上述方式工作,但它在这里不起作用。 - yas4891
@yas4891:很高兴它起作用了!现在,去问更多的问题并达到15分,这样你就可以赞同你喜欢的答案(例如这个 ;))。 - Abel
我马上就做 :-) 在家里还有另一个编程问题 :) - yas4891
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接