什么是我的xml的C#类以及如何反序列化它

时间:2016-02-09 01:32:57

标签: c# sql xml

以下是我的源码xml。我想为它编写c#类,然后反序列化它。所以我可以使用c#对象并将其保存到数据库中。 历史 - :我正在做的json反序列化很有效,但在我的xml' LastChangeId'中遇到了问题。 node可以是这个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 个答案:

答案 0 :(得分:0)

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