Diego
Diego

Reputation: 2372

c# class to xml attribute name with numbers

I am trying to XmlSerializer a class in c#.

When my XML tags are

 <Identificadores>
                  <IdMensaje>123</IdMensaje>
                  <IdMensajeAnterior >123</IdMensajeAnterior>
                  <IdOperacion >123</IdOperacion>

Is simple, having a Indentificadores class name al properties with tag

[XmlRoot(ElementName = "Identificadores")]
    public partial class Indetifiers
{

 [XmlElement(ElementName = IdMensaje")]
  public string AAA{ get; set; }
 [XmlElement(ElementName = IdMensajeAnterior")]
  public string BBB{ get; set; }
 [XmlElement(ElementName = IdOperacion")]
  public string CCC{ get; set; }
}

The problem I have is when XML tags has simbols or numbers...

 <_01:Identificadores>
                      <_01:IdMensaje>123</_01:IdMensaje>
                      <_01:IdMensajeAnterior >123</_01:IdMensajeAnterior>
                      <_01:IdOperacion >123</_01:IdOperacion>
 </_01:Identificadores>

I can not use ElementName with prefix _01:

There was an error reflecting type Identificadores

I have XML serialize error

Is there a way to use nomber and symbols to create those tags from a class?

Thanks

Upvotes: 1

Views: 2266

Answers (1)

Claudiu Tomescu
Claudiu Tomescu

Reputation: 194

Please have a look at this: https://msdn.microsoft.com/en-us/library/aa468565.aspx

The numbers you're referring to in your example are actually namespace aliases. So when you define your serialization/deserialization attributes you need to specify the namespaces prefixes in order to avoid XML serialization errors.

As I mentioned, please have a look at the link above to understand better how XML namespaces work.

Upvotes: 1

Related Questions