First, you need classes to store the Xml data. I find
Xml2CSharp.com[
^] helps. Generates the following classes from your Xml data:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="RESPONSE_STATUS")]
public class RESPONSE_STATUS {
[XmlElement(ElementName="CODE")]
public string CODE { get; set; }
[XmlElement(ElementName="MESSAGE")]
public string MESSAGE { get; set; }
}
[XmlRoot(ElementName="RESPONSE_DATA")]
public class RESPONSE_DATA {
[XmlElement(ElementName="RESPONSE_STATUS")]
public RESPONSE_STATUS RESPONSE_STATUS { get; set; }
[XmlElement(ElementName="SESSION_ID")]
public string SESSION_ID { get; set; }
[XmlElement(ElementName="CITIZEN_NUMBER")]
public string CITIZEN_NUMBER { get; set; }
}
[XmlRoot(ElementName="BIOMETRIC_VERIFICATION")]
public class BIOMETRIC_VERIFICATION {
[XmlElement(ElementName="RESPONSE_DATA")]
public RESPONSE_DATA RESPONSE_DATA { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
}
}
Next is to map the classes to the Xml data:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Xml2CSharp
{
public static class XmlConverter
{
public static T ToClass<T>(string data)
{
var response = default(T);
if (!string.IsNullOrEmpty(data))
{
var settings = new XmlReaderSettings() { IgnoreWhitespace = true };
var serializer = XmlSerializerFactoryNoThrow.Create(typeof(T));
var reader = XmlReader.Create(new StringReader(data), settings);
response = (T)Convert.ChangeType(serializer.Deserialize(reader), typeof(T));
}
return response;
}
}
public static class XmlSerializerFactoryNoThrow
{
public static Dictionary<Type, XmlSerializer> cache = new Dictionary<Type, XmlSerializer>();
private static object SyncRootCache = new object();
public static XmlSerializer Create(Type type)
{
XmlSerializer serializer;
lock (SyncRootCache)
if (cache.TryGetValue(type, out serializer))
return serializer;
lock (type)
{
serializer = XmlSerializer.FromTypes(new[] { type })[0];
}
lock (SyncRootCache) cache[type] = serializer;
return serializer;
}
}
}
To Use:
var data = XmlConverter.ToClass<BIOMETRIC_VERIFICATION>(xml);
Note: I have not run this code but should work.