使用xmlserializes反序列化XML

时间:2016-11-02 14:12:08

标签: c# deserialization xml-namespaces xmlserializer

我有这个XML:

<?xml version="1.0" encoding="UTF-8"?>
<ctm:messages xmlns:ctm="http://ctmportal.abc.se/schemas/base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <message>
    <header uid="20fefabd-9258-42fa-9004-77a01feaaeef">
      <messageType>BLABLA</messageType>
      <subscriber>ALL</subscriber>
    </header>
    <actionItem>
      <actionType>UPDATE</actionType>
      <time>2014-10-28T16:20:48</time>
    </actionItem>
    <sebContact ctmID="4477">
      <firstName>BNM</firstName>
      <lastName>abbbb</lastName>
      <telephone>8666</telephone>
      <email>vytautas.arminas@mail.com</email>
      <userId>G50130</userId>
      <title>Developer</title>
      <location ctmID="19">
        <locationName>GER</locationName>
        <locationDesc>Germany</locationDesc>
      </location>
      <contactArea>
        <contactAreaCode>47</contactAreaCode>
        <contactAreaName>Admin / IT</contactAreaName>
      </contactArea>
      <systemRole ctmID="7">
        <systemRoleName>ADMIN</systemRoleName>
      </systemRole>
      <profitCenters>
        <profitCenter>55578</profitCenter>
      </profitCenters>
      <productAreas>
        <productArea>
          <productAreaId>1</productAreaId>
          <productAreaName>Financing</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>2</productAreaId>
          <productAreaName>Other Areas within MB</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>3</productAreaId>
          <productAreaName>TB - Cash Management</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>4</productAreaId>
          <productAreaName>TB - Custody Services</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>5</productAreaId>
          <productAreaName>IB - Acquisition Finance</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>6</productAreaId>
          <productAreaName>IB - Shipping Finance</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>7</productAreaId>
          <productAreaName>IB - Export &amp; Project Finance</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>8</productAreaId>
          <productAreaName>Retail Leasing</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>9</productAreaId>
          <productAreaName>TB - Trade Finance</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>10</productAreaId>
          <productAreaName>MARKETS - Foreign Exchange</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>11</productAreaId>
          <productAreaName>IB - Corporate Finance</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>14</productAreaId>
          <productAreaName>Retail Card</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>15</productAreaId>
          <productAreaName>Life</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>22</productAreaId>
          <productAreaName>MARKETS - Capital Markets</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>23</productAreaId>
          <productAreaName>MARKETS - Equities</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>24</productAreaId>
          <productAreaName>Wealth Management</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>25</productAreaId>
          <productAreaName>TB - Fund Services</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>26</productAreaId>
          <productAreaName>IB - Structured Leasing</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>27</productAreaId>
          <productAreaName>TB - Factoring</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>28</productAreaId>
          <productAreaName>MARKETS - Commodities</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>29</productAreaId>
          <productAreaName>Retail Accounts</productAreaName>
        </productArea>
        <productArea>
          <productAreaId>30</productAreaId>
          <productAreaName>IB - DCM</productAreaName>
        </productArea>
      </productAreas>
      <relations/>
    </sebContact>
  </message>
</ctm:messages>

xml:

这样的类
    public class Header
    {
        [XmlElement(ElementName = "messageType")]
        public string MessageType { get; set; }
        [XmlElement(ElementName = "subscriber")]
        public string Subscriber { get; set; }
        [XmlAttribute(AttributeName = "uid")]
        public string Uid { get; set; }
    }

    public class ActionItem
    {
        [XmlElement(ElementName = "actionType")]
        public string ActionType { get; set; }
        [XmlElement(ElementName = "time")]
        public string Time { get; set; }
    }

    public class Location
    {
        [XmlElement(ElementName = "locationName")]
        public string LocationName { get; set; }
        [XmlElement(ElementName = "locationDesc")]
        public string LocationDesc { get; set; }
        [XmlAttribute(AttributeName = "ctmID")]
        public string CtmID { get; set; }
    }

    public class ContactArea
    {
        [XmlElement(ElementName = "contactAreaCode")]
        public string ContactAreaCode { get; set; }
        [XmlElement(ElementName = "contactAreaName")]
        public string ContactAreaName { get; set; }
    }

    public class SystemRole
    {
        [XmlElement(ElementName = "systemRoleName")]
        public string SystemRoleName { get; set; }
        [XmlAttribute(AttributeName = "ctmID")]
        public string CtmID { get; set; }
    }

    [XmlRoot(ElementName = "profitCentres")]
    public class ProfitCentres
    {
        [XmlElement(ElementName = "profitCenter")]
        public string ProfitCenter { get; set; }
    }

    public class ProductArea
    {
        [XmlElement(ElementName = "productAreaId")]
        public string ProductAreaId { get; set; }
        [XmlElement(ElementName = "productAreaName")]
        public string ProductAreaName { get; set; }
    }

    public class ProductAreas
    {
        [XmlElement(ElementName = "productArea")]
        public List<ProductArea> ProductArea { get; set; }
    }

    public class SebContact
    {
        [XmlElement(ElementName = "firstName")]
        public string FirstName { get; set; }
        [XmlElement(ElementName = "lastName")]
        public string LastName { get; set; }
        [XmlElement(ElementName = "telephone")]
        public string Telephone { get; set; }
        [XmlElement(ElementName = "email")]
        public string Email { get; set; }
        [XmlElement(ElementName = "userId")]
        public string UserId { get; set; }
        [XmlElement(ElementName = "title")]
        public string Title { get; set; }
        [XmlElement(ElementName = "location")]
        public Location Location { get; set; }
        [XmlElement(ElementName = "contactArea")]
        public ContactArea ContactArea { get; set; }
        [XmlElement(ElementName = "systemRole")]
        public SystemRole SystemRole { get; set; }
        [XmlElement(ElementName = "profitCentres")]
        public ProfitCentres ProfitCentres { get; set; }
        [XmlElement(ElementName = "productAreas")]
        public ProductAreas ProductAreas { get; set; }
        [XmlElement(ElementName = "relations")]
        public string Relations { get; set; }
        [XmlAttribute(AttributeName = "ctmID")]
        public string CtmID { get; set; }
    }

    public class Message
    {
        [XmlElement(ElementName = "header")]
        public Header Header { get; set; }
        [XmlElement(ElementName = "actionItem")]
        public ActionItem ActionItem { get; set; }
        [XmlElement(ElementName = "sebContact")]
        public SebContact SebContact { get; set; }
    }

    [XmlRoot(ElementName = "messages", Namespace = "http://ctmportal.abc.se/schemas/base")] 
    public class Messages
    {
        [XmlElement(ElementName = "message")]
        public Message[] message { get; set; }
        [XmlAttribute(AttributeName = "ctm")]
        public string ctm { get; set; }
        [XmlAttribute(AttributeName = "xsi")]
        public string xsi { get; set; }
    }

我得到消息,xsi和ctm的空值(其他元素甚至不会出现)。 不要在这里找到问题。 你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

问题解决了: 需要将命名空间添加到元素。它是空的,因为只有root元素指定了命名空间。

<b:selectBooleanCheckbox value="#{dropdownView.value}" 
           update="msgs" onchange="ajax:ajaxBean.add()" />