Deserialize Enum from Xml using Value property in C #

I am trying to write a tool for importing ONIX books in C #. I started by creating classes using Xsd2Code and got a huge file with all the properties that, after several settings, do not create any error during deserialization.

I am trying to deserialize an entire element at a time, into a large object in memory, and then do things with it (for example, store it in a database).

The way the Xsd2Code classes are generated, besides the fact that there are many properties, is a bit strange, at least for me.

Here is one of the classes that should be a property of the Product object:

public partial class NotificationType
{
    public NotificationTypeRefname refname { get; set; }
    public NotificationTypeShortname shortname { get; set; }

    public SourceTypeCode sourcetype { get; set; }

    public List1 Value { get; set; }
}

I would like to draw your attention to this line:

    public List1 Value { get; set; }

"List1" is an enumeration defined as follows:

public enum List1
{
    [System.Xml.Serialization.XmlEnum("01")]
    Item01,

    [System.Xml.Serialization.XmlEnum("02")]
    Item02, etc...

, . .

XmlEnum ( "NotificationType" ) ..... !

:

var p = new Product();
XmlSerializer pserializer = new XmlSerializer(p.GetType());
object pDeserialized = pserializer.Deserialize(reader);
p = (Product) pDeserialized;

XML:

<NotificationType>03</NotificationType>

, # Product:

public NotificationType NotificationType { get; set; }

:

public List1 NotificationType { get; set; }

"Item03", , XML. , , "" NotificationType Item01 ( Enum).

SO -, Value (Strings), Enums. - ?

. , . .

+5
2

[System.Xml.Serialization.XmlTextAttribute()] public List1 Value { get; set; }.

+1

:

public partial class NotificationType
{
    public NotificationTypeRefname refname { get; set; }
    public NotificationTypeShortname shortname { get; set; }
    public SourceTypeCode sourcetype { get; set; }

    public List1 Value { get {
        return (List1)Enum.Parse(typeof(List1), 
            Enum.GetName(typeof(List1), int.Parse(List1Value) - 1));
    }}

    [XmlText]
    public string List1Value { get; set; }
}

[]

  • XmlText, :

    'ConsoleApplication1.NotificationType. XmlText 'ConsoleApplication1.NotificationType.Value' ConsoleApplication1.List1 .

  • ,

, , XmlText Value, XmlIgnoreAttribute. , XmlText , .

public class NotificationType
{
    [XmlIgnore] // required
    public NotificationTypeRefname refname { get; set; }
    [XmlIgnore] // required
    public NotificationTypeShortname shortname { get; set; }
    [XmlIgnore] // required
    public SourceTypeCode sourcetype { get; set; }

    [XmlText] // required
    public List1 Value { get; set; }
}
+2

All Articles