当属性中包含命名空间前缀时,XmlSerializer 会引发 InvalidOperationException
我尝试读取包含以下元素的 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>
I try to read an XML file that contains the following element:
<ho:CODED-TYPE ho:BASE-DATA-TYPE="A_UINT16" CATEGORY="STANDARD-LENGTH-TYPE" ENCODING="UNSIGNED">
My class to describe this node looks like that:
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);
};
I get an InvalidOperationException from XmlSerializer.ctor telling me:
"Ungültiges Namenszeichen in 'ho:BASE-DATA-TYPE'." (this could be translated as "invalid character in: 'ho:BASE-DATA-TYPE'").
I also tried the following:
[XmlAttribute("BASE-DATA-TYPE", Namespace="http://www.asam.net/xml")]
property String^ BaseDataType;
But this didn't work either. This time without the error message but the unit test fails telling me, that the property is still set to "null".
I am completely stuck with this. So any help is appreciated
thanks in advance
EDIT: some more 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由OP编辑后重写了整个答案
我最初对错误的理解是错误的。该错误是在序列化程序初始化时引发的,而不是在读取 XML 时引发的。名称中不能使用冒号
:
。如果指定命名空间,请勿指定前缀。实际上,您几乎不会指定前缀(它只是命名空间的占位符)。执行此操作后,您已经注意到该值最终为
null
。原因是序列化器默认使用不合格的属性。如果您有限定的属性,则假定属性名称空间与元素的名称空间不同。这会起作用:这似乎是一个奇怪的错误。这是 MSDN 上的对此有些类似的报告,它帮助我找到了解决方案。只需将属性标记为合格即可。以下内容适用于您的输入 XML(请注意
XmlSchemaForm.Qualified
):rewritten entire answer after edit by OP
My original understanding of the error was wrong. The error is thrown on the initialization of the serializer, not when you read your XML. You cannot use a colon
:
in a name. If you specify a namespace, do not specify the prefix. Actually, you hardly ever specify the prefix (which is just a placeholder for the namespace).After doing so, you already noticed that the value ends up
null
. The reason is that the serializer defaults to unqualified attributes. If you have qualified attributes, it assumes the attribute namespace is different than the element's namespace. This will work:This seems an odd bug. Here's somewhat similar report on thisn at MSDN, which helped me to the solution. Just mark the attribute as qualified. The following works with your input XML (note
XmlSchemaForm.Qualified
):