我有一个EventType和Events类来序列化/反序列化。在Xml for Events中有一个名为"类型"的XmlElement。
<?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>
但是我也有一个带有EventTypes列表和不同xmlelement名称的Xml&#34; eventtypes&#34;。
<?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>
我在下面使用这个。但是当我想反序列化eventtypes Xml时,由于XmlRoot属性的标题,我有一个异常。
[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; }
}
我知道我可以写两个具有相同属性的不同类。但我想知道是否还有其他方法可以做到。
答案 0 :(得分:0)
XmlRootAttribute
属性仅控制用于类型的元素名称(当该类型的枚举(或该类型的可枚举))是文档的根元素时。否则,引用该类型的属性的属性名称或XmlElementAttribute.ElementName
值将控制XML元素名称。因此,当包含在不同的外部类中时,EventType
类不存在具有不同元素名称的冲突,如下所示:
[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; }
}
示例fiddle。
顺便提一下,您可以从该类中删除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; }
}