C#类到带有数字的xml属性名称

时间:2018-09-21 15:44:34

标签: c# xml class xml-serialization

我正在尝试在C#中使用XmlSerializer一个类。

当我的XML标签是

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

很简单,具有一个带有标签的Indentificadores类名al属性

[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; }
}

我遇到的问题是XML标签包含符号或数字时...

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

我不能使用带有前缀_01:的ElementName

出现一个反映类型Identificadores

的错误

我遇到XML serialize错误

是否可以使用Nomber和符号从类中创建这些标记?

谢谢

1 个答案:

答案 0 :(得分:1)

请查看以下内容:https://msdn.microsoft.com/en-us/library/aa468565.aspx

您在示例中引用的数字实际上是名称空间别名。因此,在定义序列化/反序列化属性时,需要指定名称空间前缀,以避免XML序列化错误。

正如我所提到的,请查看上面的链接以更好地了解XML名称空间的工作方式。