XML反序列化返回" some"空值

时间:2015-11-11 15:14:37

标签: c# xml deserialization

这是我的XML:

<Events>
  <Event>
    <content_id>6442452774</content_id>
    <content_title>Title of the event</content_title>
    <content_html>
<Date>2015-11-18</Date>
<EventType>Events</EventType>
<Description>
<p>this is an "event"</p>
</Description>
<Speakers>speaker1 LLC<br />speaker2<br />Jspeaker3</Speakers>
<Time>5:30 - 6:00pm Registration<br />6:00 - 7:00pm Panel Discussion<br />7:00 - 8:00pm Networking Reception</Time>
<Where></Where>
<RegistrationInfo>Please contact <a href="mailto:events@events.com">events@events.com</a> to register for this event.</RegistrationInfo>
<Registration>false</Registration>
</content_html>
    <date_created>2015-10-24T14:24:12.333</date_created>
    <folder_id>262</folder_id>
    <content_teaser>this is the content "can you display it."</content_teaser>
    <content_text>text of the vent "more text" a lot of text here </content_text>
    <end_date>2015-11-19T21:35:00</end_date>
    <content_type>1</content_type>
    <template_id>43</template_id>
    <content_status>A</content_status>
  </Event>
<Event>.... Other events   </Event>
<Events>

这是我的课程:

 public class Serializable_Events
    {
        [XmlElement("Event")]
        public List<Serializable_Event> EventList = new List<Serializable_Event>();
    }
    public class Serializable_Event
    {
        [XmlElement("content_id")]
        public string content_id { get; set; }

        [XmlElement("content_title")]
        public string content_title { get; set; }

        [XmlElement("content_html")]
        public Serializable_Event_ContentHTML ContentHTML { get; set; }
        [XmlText]
        public string content_teaser { get; set; }
        [XmlElement("content_text")]
        public string content_text { get; set; }
    }
    public class Serializable_Event_ContentHTML
    {
        [XmlElement("Date")]
        public string Date { get; set; }

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

        [XmlElement("Description")]
        public string Description { get; set; }
        [XmlElement("Speakers")]
        public string Speakers { get; set; }

        [XmlElement("Time")]
        public string Time { get; set; }


        [XmlElement("Where")]
        public string Where { get; set; }

        [XmlElement("RegistrationInfo")]
        public string RegistrationInfo { get; set; }

        [XmlElement("Registration")]
        public bool Registration { get; set; }

        //ignored html tags
        [XmlIgnore]
        public string p { get; set; }
        [XmlIgnore]
        public string br { get; set; }
        [XmlIgnore]
        public string a { get; set; }

    }

实现:

XmlSerializer ser = new XmlSerializer(typeof(Serializable_Events));
            var data = (Serializable_Events)ser.Deserialize(new StreamReader(@"events.xml"));

我的问题是某些属性为空,有些属性不是(请参见屏幕截图)enter image description here

1 个答案:

答案 0 :(得分:2)

那些空的,如<Description>,是由于格式错误 XML

您将HMTL直接存储在XML中,文本遍布整个地方,而序列化程序并不期望这样;此外,您告诉序列化程序使用XmlIgnore忽略HTML标记,XmlIgnore用于具有正确格式的XML的XML标记。这是XmlIgnore

的错误用法
  

包含非XML标记的所有XML都应包含在CDATA sections中;这将解决您的问题。此外,您也可以删除所有<Events> <Event> <content_id>6442452774</content_id> <content_title>Title of the event</content_title> <content_html> <Date>2015-11-18</Date> <EventType>Events</EventType> <Description> <![CDATA[<p>this is an ""event""</p>]]> </Description> <Speakers> <![CDATA[speaker1 LLC<br />speaker2<br />Jspeaker3]]> </Speakers> <Time> <![CDATA[5:30 - 6:00pm Registration<br />6:00 - 7:00pm Panel Discussion<br />7:00 - 8:00pm Networking Reception]]> </Time> <Where></Where> <RegistrationInfo> <![CDATA[Please contact <a href='mailto:events@events.com'>events@events.com</a> to register for this event.]]> </RegistrationInfo> <Registration>false</Registration> </content_html> <date_created>2015-10-24T14:24:12.333</date_created> <folder_id>262</folder_id> <content_teaser>this is the content 'can you display it.'</content_teaser> <content_text>text of the vent 'more text' a lot of text here </content_text> <end_date>2015-11-19T21:35:00</end_date> <content_type>1</content_type> <template_id>43</template_id> <content_status>A</content_status> </Event> </Events>" 代码,因为它不需要。

您的XML应如下所示:

private ServiceRegistration serviceReg;

@Activate
public void activate(BundleContext context) {
  String sysProp = System.getProperty("doNotActivate");
  if (sysProp != null && Boolean.parse(sysProp)) {
    return;
  }

  serviceReg = context.registerService(....);
}

@Deactivate
public void deactivate() {
  if (serviceReg != null) {
    serviceReg.unregister();
  }
}