Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XmlSerializer在属性中具有命名空间前缀时引发InvalidOperationException_C#_.net_C++ Cli_Xmlserializer - Fatal编程技术网

C# XmlSerializer在属性中具有命名空间前缀时引发InvalidOperationException

C# XmlSerializer在属性中具有命名空间前缀时引发InvalidOperationException,c#,.net,c++-cli,xmlserializer,C#,.net,C++ Cli,Xmlserializer,我尝试读取包含以下元素的XML文件: <ho:CODED-TYPE ho:BASE-DATA-TYPE="A_UINT16" CATEGORY="STANDARD-LENGTH-TYPE" ENCODING="UNSIGNED"> 我从XmlSerializer.ctor收到一个InvalidOperationException,告诉我: “ho:BASE-DATA-TYPE中的Ungültiges Namenszeichen.”(这可以翻译为“ho:BASE-DATA-TYPE中

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

<ho:CODED-TYPE ho:BASE-DATA-TYPE="A_UINT16" CATEGORY="STANDARD-LENGTH-TYPE" ENCODING="UNSIGNED">
我从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>

编码速度
在此系统内对速度值进行编码。
16
在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"...>

您同时下注于不同的马:;-)是的,我知道:-(在那里很难找到答案,而且我的单元测试条仍然是红色的,所以我吓得魂不附体:-(Wooohaaa。那篇文章中有一个错误:-(请稍等。@yas4891:请显示输入XML的确切声明,包括名称空间的声明方式。此外,请显示一个失败的最小化输入示例。您使用的是架构验证器还是涉及DTD?您真的希望我发布完整的XmlSource?大约7KB。我们同意根元素+所讨论元素的父元素?到目前为止,我的序列化对文档的其余部分有效,但对“错误”命名空间中的该属性无效。我设法获得了另一个属性(“xml:lang”)按照上面描述的方式工作,但在这里不起作用。好的。我添加了更多的XML代码。这是ASAMGREEN BAR的FIBEX CAN示例!非常感谢!你就是那个人!当然。我是认真的!谢谢!
<!-- 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"...>
[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; }
}