代码之家  ›  专栏  ›  技术社区  ›  Amrik

我的xml的C#类是什么?如何反序列化它[已关闭]

  •  -3
  • Amrik  · 技术社区  · 10 年前

    下面是我的源xml。我想为它编写c类,然后反序列化它。所以我可以使用c对象并将其保存到数据库中。 历史-:我在做json反序列化,效果很好,但在我的xml中遇到了问题。“LastChangeId”节点可能是这个xml的一部分,有时可能根本不存在,有时可能有多个节点。这个问题干扰了我的json解析。我必须切换回xml。任何帮助都是值得的。

    <FundingSource    >
      <ClientAccountPaySourceId>16</ClientAccountPaySourceId>
      <ClientAccountId>67</ClientAccountId>
      <ClientAccountName>Default Account</ClientAccountName>
      <PrimaryPartyId>62</PrimaryPartyId>
      <PrimaryRoleId>1290</PrimaryRoleId>
      <TenderTypeId>3</TenderTypeId>
      <TenderTypeName>Credit Card</TenderTypeName>
      <TenderInterfaceName>Credit Card</TenderInterfaceName>
      <CreditCareTypeName>Visa</CreditCareTypeName>
      <ChargeAccountMask>1111</ChargeAccountMask>
      <ExpirationDate>04/20</ExpirationDate>
      <BillingAccountName>Joe Montana</BillingAccountName>
      <BillingStreet>1235 Main St</BillingStreet>
      <BillingCity>Pleasanton</BillingCity>
      <BillingPostalCode>94588</BillingPostalCode>
      <BillingCountry>US</BillingCountry>
      <BillingTelephone>1231234567</BillingTelephone>
      <DisplayOrder>1</DisplayOrder>
      <UseForRecurring>true</UseForRecurring>
      <UseForNonRecurring>true</UseForNonRecurring>
      <IsActive>true</IsActive>
      <Invalid>false</Invalid>
      <ChargeAccountToken>VC84632147254611111111</ChargeAccountToken>
      <IsExternal>false</IsExternal>
      <LastChangeId   >
        <ClientAccountPaySourceId>16</ClientAccountPaySourceId>
        <TimeUtc>2016-02-02 01:04:16</TimeUtc>
        <TimeLocal>2016-02-01 17:04:16</TimeLocal>
        <UserName>Josh.Lyon</UserName>
        <PartyId>20</PartyId>
        <RoleId>1160</RoleId>
        <BusinessUnitCode>2</BusinessUnitCode>
        <EndpointKey>default</EndpointKey>
      </LastChangeId>
    </FundingSource>
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Community Mohan Dere    9 年前
    public class FundingSource {
    public int ClientAccountPaySource {get; set;}
    public int ClientAccountId {get; set;}
    public string ClientAccountName {get; set;}
    ...
    }
    //Use it like so:
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
    
    x.Serialize(Console.Out, new FundingSource() { ClientAccountPaySource=1, ClientAccountId=100, ClientAccountName="Name"});
    

    有关详细信息,请参阅 MSDN Page on XML Serialization 或相关问题的数量 like this one