parse XML

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Linq;
using System.IO;
					
public class Program
{
	public static void Main()
	{
		var response = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
			"<soap:Body>" +
				"<AccountStatementResponse xmlns=\"http://formalityzues.com/\">" +
					"<AccountStatementResult><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\" ?><ViewStatementResponse><TransactionItems><RefNumber>2</RefNumber><TransactionDate>31/05/2017</TransactionDate><ValueDate>31/05/2017</ValueDate><Amount>400</Amount><Narration>CHIEF ATTENDANT</Narration><TransactionType>CR</TransactionType><BalanceAfterTransaction>57003602.16</BalanceAfterTransaction></TransactionItems><TransactionItems><RefNumber>3</RefNumber><TransactionDate>31/05/2017</TransactionDate><ValueDate>31/05/2017</ValueDate><Amount>3800</Amount><Narration>HEAD QUATERS</Narration><TransactionType>CR</TransactionType><BalanceAfterTransaction>57007402.16</BalanceAfterTransaction></TransactionItems></ViewStatementResponse>]]></AccountStatementResult>" +
				"</AccountStatementResponse>" +
			"</soap:Body>" +
			"</soap:Envelope>";
		var soap = XDocument.Parse(response);
		var data = soap.Descendants("{http://formalityzues.com/}AccountStatementResult").First();
		var reader = new StringReader(data.Value);
		var serializer = new XmlSerializer(typeof(Model));
		var model = serializer.Deserialize(reader) as Model;
		model.Dump();
	}
	
	[XmlRoot("ViewStatementResponse")]
	public class Model
	{
		[XmlElement("OpeningBalance")]
		public string OpeningBalance { get; set; }
	
		[XmlElement("ClosingBalance")]
		public string ClosingBalance { get; set; }
	
		[XmlElement("TransactionItems")]
		public List<TransactionDetails> TransactionItems { get; set; }
	}
	
	public class TransactionDetails
	{
		[XmlElement("RefNumber")]
		public string RefNumber { get; set; }
	
		[XmlElement("TransactionDate")]
		public string TransactionDate { get; set; }
	
		[XmlElement("ValueDate")]
		public string ValueDate { get; set; }
	
		[XmlElement("Amount")]
		public string Amount { get; set; }
	
		[XmlElement("Narration")]
		public string Narration { get; set; }
	
		[XmlElement("TransactionType")]
		public string TransactionType { get; set; }
	
		[XmlElement("BalanceAfterTransaction")]
		public string BalanceAfterTransaction { get; set; }
	}

}
;