简体   繁体   中英

XmlSerializer throws InvalidOperationException when having a namespace prefix in the attribute

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>

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 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"...>

This seems an odd bug. Here's somewhat similar report on this n at MSDN, which helped me to the solution. Just mark the attribute as qualified. The following works with your input XML (note 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM