我该如何使用C#对XML文档进行反序列化,我只对ComplianceInfo部分感兴趣?

时间:2019-03-08 12:28:37

标签: c# xml linq

我如何使用C#解析此xml文档,我仅对ComplianceInfo部分感兴趣?

<RootDTO>      
       <SbisInfo>
          <Errors />
          <InfoMessages>
             <InfoMessageDTO>
                <Message>Service completed successfully</Message>
             </InfoMessageDTO>
          </InfoMessages>
          <Customer>
             <BPID>150242138</BPID>
             <ComplianceInfo>
                <ComplianceStatusCode>007</ComplianceStatusCode>
                <ComplianceStatusInd>Y</ComplianceStatusInd>
                <FraudConsentGivenInd>Y</FraudConsentGivenInd>
                <UndesirableStatusCode>009</UndesirableStatusCode>
                <PepStatusCode>007</PepStatusCode>
                <KycStatusCode>001</KycStatusCode>
                <CraStatusCode>004</CraStatusCode>
             </ComplianceInfo>
          </Customer>
       </SbisInfo>
     </RootDTO>

1 个答案:

答案 0 :(得分:1)

您可以使用ComplianceInfo和XPath选择器从XmlDocument节点获取数据,如下所示:

string xml = @"your-xml-here";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode node = doc.SelectSingleNode("/RootDTO/SbisInfo/Customer/ComplianceInfo");

string ComplianceStatusCode = node["ComplianceStatusCode"].InnerText;
string ComplianceStatusInd = node["ComplianceStatusInd"].InnerText;
string FraudConsentGivenInd = node["FraudConsentGivenInd"].InnerText;
string UndesirableStatusCode = node["UndesirableStatusCode"].InnerText;
string PepStatusCode = node["PepStatusCode"].InnerText;
string KycStatusCode = node["KycStatusCode"].InnerText;
string CraStatusCode = node["CraStatusCode"].InnerText;