gpt4 book ai didi

c# - 当属性中有命名空间前缀时,XmlSerializer 抛出 InvalidOperationException

转载 作者:行者123 更新时间:2023-11-30 17:21:32 24 4
gpt4 key购买 nike

我尝试读取包含以下元素的 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 告诉我:

“'ho:BASE-DATA-TYPE' 中的 Ungültiges Namenszeichen。” (这可以翻译为“无效字符:'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>

最佳答案

OP 编辑​​后重写了整个答案

我原来对错误的理解是错误的。错误是在序列化器初始化时抛出的,不是是在您读取 XML 时抛出的。您不能在名称中使用冒号 :。如果指定 namespace ,请不要指定前缀。实际上,您几乎从未指定前缀(它只是命名空间的占位符)。

这样做之后,您已经注意到该值最终为 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"...>

这似乎是一个奇怪的错误。这是 somewhat similar report on this n 在 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; }
}

关于c# - 当属性中有命名空间前缀时,XmlSerializer 抛出 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3342549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com