Deserialize Xml Using Same Object With Different Element Name

三世轮回 提交于 2019-12-25 04:52:08

问题


I have an EventType and Events classes to serialize/deserialize. In Xml for Events there is a XmlElement named "type".

<?xml version="1.0" encoding="UTF-8"?>
<events type="array">
   <event>
      <where />
      <project-users-can-edit type="boolean">false</project-users-can-edit>
      <description />
      <attending-user-ids type="integer">164316</attending-user-ids>
      <notify-user-names>Levent A.</notify-user-names>
      <attending-user-names>Levent A.</attending-user-names>
      <status>active</status>
      <owner>
         <first-name>Levent</first-name>
         <id type="integer">164316</id>
         <last-name>Arık</last-name>
      </owner>
      <reminders type="array" />
      <notify-user-ids type="integer">164316</notify-user-ids>
      <start>2015-06-22T00:00</start>
      <repeat />
      <all-day type="boolean">true</all-day>
      <id type="integer">608945</id>
      <end>2015-06-22T23:59</end>
      <show-as-busy type="boolean">false</show-as-busy>
      <last-changed-on type="date">2015-07-08T09:20:37Z</last-changed-on>
      <privacy>
         <type>company</type>
      </privacy>
      <attendees-can-edit type="boolean">false</attendees-can-edit>
      *<type>
         <name>Yıllık İzin</name>
         <id type="integer">104109</id>
         <color>C65518</color>
      </type>*
      <title>Yıllıkİzin</title>
   </event>
</events>

But I also have an Xml with the list of EventTypes and with the different xmlelement name "eventtypes".

<?xml version="1.0" encoding="UTF-8"?>
<eventtypes type="array">
   <eventtype>
      <name>Diğer</name>
      <id type="integer">104285</id>
      <color>E7C342</color>
   </eventtype>
   <eventtype>
      <name>Hastalık</name>
      <id type="integer">104284</id>
      <color>399A5A</color>
   </eventtype>
   <eventtype>
      <name>Mazeret İzni</name>
      <id type="integer">104110</id>
      <color>633C9C</color>
   </eventtype>
   <eventtype>
      <name>ResmiTatil</name>
      <id type="integer">104286</id>
      <color type="integer">737173</color>
   </eventtype>
   <eventtype>
      <name>Yıllık İzin</name>
      <id type="integer">104109</id>
      <color>C65518</color>
   </eventtype>
</eventtypes>

I am using below class for this. But when I want to deserialize eventtypes Xml I have an exception because of title of XmlRoot attribute.

[Serializable]
[XmlRoot("type")]
public class EventType
{
    public EventType()
    {

    }
    [XmlElement("color")]
    public string Color { get; set; }
    [XmlElement("id")]
    public string Id { get; set; }
    [XmlElement("name")]
    public string Name { get; set; }

}

I know i can write 2 different classes with same properties. But i wonder if there is another way to do it.


回答1:


The XmlRootAttribute attribute only controls the element name used for a type when that type (or an enumerable of that type) is the root element of the document. Otherwise, the property name or XmlElementAttribute.ElementName value of the property referring to the type controls the XML element name. Thus, there is no conflict having different element names for your EventType class when contained in different outer classes, like so:

[XmlRoot("eventtypes")]
public class EventTypeList
{
    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlElement("eventtype")]
    public List<EventType> EventTypes { get; set; }
}

[XmlRoot("events")]
public class EventList
{
    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlElement("event")]
    public List<Event> Events { get; set; }
}

[XmlRoot("event")]
public class Event // Prototype implementation only, many properties omitted.
{
    [XmlElement("title")]
    public string Title { get; set; }

    // Remainder omitted for brevity.

    [XmlElement("type")]
    public EventType Type { get; set; }
}

Example fiddle.

Incidentally, you can remove the Serializable attribute from you class since it's only useful for binary serialization:

[XmlRoot("type")]
public class EventType
{
    public EventType() { }
    [XmlElement("color")]
    public string Color { get; set; }
    [XmlElement("id")]
    public string Id { get; set; }
    [XmlElement("name")]
    public string Name { get; set; }
}


来源:https://stackoverflow.com/questions/31490563/deserialize-xml-using-same-object-with-different-element-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!