简体   繁体   English

对可能是不同类型的xml元素进行反序列化

[英]Deserialization an xml element that could be different types

I need to deserialize the flowing xml in c# 我需要在C#中反序列化流动的xml

<Show>
   <status>Canceled</status>
</Show>

<Show>
    <status>2</status>
</Show>

my class is 我班是

[XmlRoot("Show")]`
public class Show
{
   [XmlElement(ElementName = "status")]
   public object status { get; set; }
}

and it works but i would like to deserialize it into an enum where in this example cancel is equal 2 它可以工作,但我想反序列化为枚举,在此示例中,cancel等于2

public enum ShowStatus
{
   [XmlEnum("Canceled")]
   Canceled = 2
}

is there any way to do that without parse the public object status { get; set; } 有什么方法可以在不解析public object status { get; set; }情况下做到这一点public object status { get; set; } public object status { get; set; } public object status { get; set; } string value to enum public object status { get; set; }要枚举的字符串值

If you want to Deserialize the Enum using the name or the integer you can decorate the Enum with XmlEnum attribute and supply the integer. 如果要使用名称或整数对Enum进行Deserialize则可以使用XmlEnum属性装饰Enum并提供该整数。

This will deserialise "Canceled" and "2" as your Enum. 这将反序列化“取消”和“ 2”作为您的枚举。

Example: 例:

[XmlRoot("Show")]
public class Show
{
    [XmlElement(ElementName = "status")]
    public ShowStatus status { get; set; }
}

public enum ShowStatus
{
    [XmlEnum("2")]
    Canceled = 2
}

Test xml: 测试xml:

<?xml version="1.0"?>
<ArrayOfShow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Show>
    <status>Canceled</status>
  </Show>
  <Show>
    <status>2</status>
  </Show>
</ArrayOfShow>

you cannot use object directly, XmlSerializer simply refuses to work with generics. 您不能直接使用object ,XmlSerializer只是拒绝使用泛型。 Objects still have an underlying type, and when its de-serialzied, you have no idea what that type is. 对象仍然具有基础类型,并且在反序列化时,您不知道该类型是什么。 I made a workaround for it here but its not very pretty, or very useful. 我在这里做了一个解决方法,但是它不是很漂亮,或者非常有用。

You can custom implement IXmlSerializable , but that is a headache and a half. 您可以自定义实现IXmlSerializable ,但这IXmlSerializable令人头疼。

I am assuming that the source of the problem is that you are taking this in as input from another source, and the field could be either integer or text, which you want to store as an enumerable. 我假设问题的根源是您将其作为其他来源的输入,并且该字段可以是整数或文本,您希望将其存储为可枚举值。 You are probably better of de-serializing it into a string, then simply parsing it later, afterwards. 您最好将其反序列化为字符串,然后再稍后对其进行解析。 That would probably be the easiest way to do it. 那可能是最简单的方法。 Everything else I can find is either about deserializing only the string values, or only the integer values, I have no idea if either of them can handle both forms of data, but it seems unlikely. 我能找到的所有其他内容都是关于仅对字符串值或仅对整数值进行反序列化,我不知道它们中的任何一个是否都可以处理两种形式的数据,但这似乎不太可能。

This Function will probably help you out a lot when it comes to that, it looks like it can handle the string or the numerical value, but I dont think XmlSerializer can use it. 就此而言,此函数可能会为您提供很多帮助,它看起来可以处理字符串或数值,但是我不认为XmlSerializer可以使用它。 You would be better off with a string "dummy" that can save back to the Enum property using the Parse function. 最好使用字符串“ dummy”,该字符串可以使用Parse函数保存回Enum属性。 see this stackoverflow question for an example of the dummy property. 请参阅此stackoverflow问题以获取dummy属性的示例。

Example

Generally speaking it would look something like this: 一般来说,它看起来像这样:

[XmlRoot("Show")]`
public class Show
{
   [XmlIgnore()]
   public ShowStatus status { get; set; }

   [XmlElement(ElementName = "status")]
   public string StatusString
   {
       get { return status.ToString(); }
       set { status = Enum.Parse(ShowStatus, value); }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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