I have a response from a soap service. It looks something like:
<ArrayOfPaymentHistory_ST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<PaymentHistory_ST>
<tTRANSDATE>2015-01-05T08:58:17</tTRANSDATE>
<tPAIDTO>2015-02-01T00:00:00</tPAIDTO>
<dAMOUNT>110</dAMOUNT>
<dBALANCE>0</dBALANCE>
<TRANSKIND>Auto CC Payment</TRANSKIND>
<csUnit>Lee</csUnit>
</PaymentHistory_ST>
<PaymentHistory_ST>
<tTRANSDATE>2015-01-01T08:58:17</tTRANSDATE>
<tPAIDTO>2015-01-01T00:00:00</tPAIDTO>
<dAMOUNT>-110</dAMOUNT>
<dBALANCE>110</dBALANCE>
<TRANSKIND>Rent Posted</TRANSKIND>
<csUnit>Lee</csUnit>
</PaymentHistory_ST>
</ArrayOfPaymentHistory_ST>
I have defined a model like this:
public class payment_history
{
public DateTime tTRANSDATE { get; set; }
public DateTime tPAIDTO { get; set; }
public double dAMOUNT { get; set; }
public double dBALANCE { get; set; }
public string TRANSKIND { get; set; }
}
I am trying to parse this XML response and make a list of object of type payment_history. I will then send this object on my markup and display in a table. I found many post on XML parsing using XMLReader. I am still not able to find a good solution that can do this elegantly.
Here's my c# code.
List<payment_history> ph = new List<payment_history>();
payment_history p = new payment_history();
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
using (XmlReader reader = XmlReader.Create(new StringReader(soapResult)))
{
#region parseXML
while (reader.Read())
{
foreach (var el in reader.ReadOuterXml())
{
if (reader.IsStartElement())
{
//return only when you have START tag
switch (reader.Name.ToString())
{
case "tTRANSDATE":
p.tTRANSDATE = Convert.ToDateTime(reader.ReadString());
break;
case "tPAIDTO":
p.tPAIDTO = Convert.ToDateTime(reader.ReadString());
break;
case "dAMOUNT":
p.dAMOUNT = Convert.ToDouble(reader.ReadString());
break;
case "dBALANCE":
p.dBALANCE = Convert.ToDouble(reader.ReadString());
break;
case "TRANSKIND":
p.TRANSKIND = reader.ReadString();
break;
}
ph.Add(p);
}
}
}
#endregion
}
}
}
return ph;
Can someone guide through to make this work? I am new to c#. Any kind of help would be much appreciated.
I tried following LINQ2XML. But doesn't return anything!!
ph = XDocument.Parse(soapResult)
.Descendants("PaymentHistory_ST")
.Select(g => new payment_history
{
tTRANSDATE = (DateTime)g.Element("tTRANSDATE"),
tPAIDTO = (DateTime)g.Element("tPAIDTO"),
dAMOUNT = (double)g.Element("dAMOUNT"),
dBALANCE = (double)g.Element("dBALANCE"),
TRANSKIND = (string)g.Element("TRANSKIND")
}).ToList();
Use
XDocument doc = XDocument.Load(response.GetResponseStream());
XNamespace df = "http://tempuri.org/";
List<payment_history> ph = doc.Descendants(df + "PaymentHistory_ST")
.Select(g => new payment_history
{
tTRANSDATE = (DateTime)g.Element(df + "tTRANSDATE"),
tPAIDTO = (DateTime)g.Element(df + "tPAIDTO"),
dAMOUNT = (double)g.Element(df + "dAMOUNT"),
dBALANCE = (double)g.Element(df + "dBALANCE"),
TRANSKIND = (string)g.Element(df + "TRANSKIND")
}).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.