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 告诉我:

“‘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>
4

1 回答 1

2

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上关于这个 n 的一些类似的报告,它帮助我找到了解决方案。只需将属性标记为合格。以下适用于您的输入 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; }
}
于 2010-07-27T10:15:14.877 回答