我正在尝试从API中提取XML响应。我的XML响应如下所示。我试过不同的方法。能帮助我从XMl响应中读取和提取单个节点吗?
例如:我想从下面的XML响应中提取节点。
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<RichDBDS><TrxDetailCard><TRX_HD_Key>5437845</TRX_HD_Key> <Invoice_ID>189809</Invoice_ID><Date_DT>3/24/2016 1:34:44 PM</Date_DT><Merchant_Key>2202</Merchant_Key><Reseller_Key>2</Reseller_Key><TUser_Name_VC>ITSTEST.ISMI</TUser_Name_VC><Processor_ID>CC Processor</Processor_ID><TRX_Settle_Key></TRX_Settle_Key><Tip_Amt_MN></Tip_Amt_MN><Approval_Code_CH>24032016013444</Approval_Code_CH><Auth_Amt_MN>184.99</Auth_Amt_MN><IP_VC>66.55.53.68</IP_VC><Account_Type_CH>MANUAL</Account_Type_CH><Last_Update_DT></Last_Update_DT><Orig_TRX_HD_Key></Orig_TRX_HD_Key><Settle_Date_DT></Settle_Date_DT><Settle_Flag_CH>FALSE</Settle_Flag_CH><Trans_Type_ID>Sale</Trans_Type_ID><Void_Flag_CH>FALSE</Void_Flag_CH><CustomerID></CustomerID><AVS_Resp_CH>Y</AVS_Resp_CH><CV_Resp_CH>M</CV_Resp_CH><Host_Ref_Num_CH>0b144718-3597-4cfb-a2b5-058679510f29</Host_Ref_Num_CH><Zip_CH>30328</Zip_CH><Acct_Num_CH>1111</Acct_Num_CH><Total_Amt_MN>184.99</Total_Amt_MN><Exp_CH>0917</Exp_CH><Name_on_Card_VC>Kenneth Boler</Name_on_Card_VC><Type_CH>VISA</Type_CH><Cash_Back_Amt_MN></Cash_Back_Amt_MN><Result_CH>0</Result_CH><Result_Txt_VC>approval</Result_Txt_VC><Trans_Status>Settled</Trans_Status><PO_Num></PO_Num></TrxDetailCard></RichDBDS>
答案 0 :(得分:0)
使用XML Linq尝试如下代码。我使用了Load(FILENAME或URL)方法,但您也可以使用Parse(STRING)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants().Where(x => x.Name.LocalName == "TrxDetailCard").Select(y => new {
TRX_HD_Key = (int)y.Element(y.Name.Namespace + "TRX_HD_Key"),
Invoice_ID = (int)y.Element(y.Name.Namespace + "Invoice_ID"),
Date_DT = (DateTime)y.Element(y.Name.Namespace + "Date_DT"),
Merchant_Key = (int)y.Element(y.Name.Namespace + "Merchant_Key")
}).ToList();
}
}
}