Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my code is mentioned below where i load the data into xml document.

XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(resty.Data);
doc.LoadXml(xml);

innser xml contains:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><VerifyFingerPrintsResponse xmlns="http://NADRA.Biometric.Verification"><VerifyFingerPrintsResult><?xml version="1.0" encoding="utf-16"?>

<BIOMETRIC_VERIFICATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <RESPONSE_DATA>

    <RESPONSE_STATUS>

      <CODE>175</CODE>

      <MESSAGE>transaction id already exist</MESSAGE>

    </RESPONSE_STATUS>

    <SESSION_ID>12313</SESSION_ID>

    <CITIZEN_NUMBER>4220106957335</CITIZEN_NUMBER>

  </RESPONSE_DATA>

</BIOMETRIC_VERIFICATION></VerifyFingerPrintsResult></VerifyFingerPrintsResponse></s:Body></s:Envelope>



inner text :


<?xml version="1.0" encoding="utf-16"?>

<BIOMETRIC_VERIFICATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <RESPONSE_DATA>

    <RESPONSE_STATUS>

      <CODE>175</CODE>

      <MESSAGE>transaction id already exist</MESSAGE>

    </RESPONSE_STATUS>

    <SESSION_ID>12313</SESSION_ID>

    <CITIZEN_NUMBER>4220106957335</CITIZEN_NUMBER>

  </RESPONSE_DATA>

</BIOMETRIC_VERIFICATION>


What I have tried:

i want to retrieve data from above xml kindly  help me please
how i will retrieve data from above xml
Posted
Updated 2-Mar-17 4:13am

First, you need classes to store the Xml data. I find Xml2CSharp.com[^] helps. Generates the following classes from your Xml data:
C#
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:
C#
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;
        }
    }

    // ref: http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor/39642834#39642834
    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) //multiple variable of type of one type is same instance
            {
                //constructor XmlSerializer.FromTypes does not throw the first chance exception           
                serializer = XmlSerializer.FromTypes(new[] { type })[0];
                //serializer = XmlSerializerFactoryNoThrow.Create(type);
            }

            lock (SyncRootCache) cache[type] = serializer;
            return serializer;
        }
    }
}
To Use:
C#
var data = XmlConverter.ToClass<BIOMETRIC_VERIFICATION>(xml);
Note: I have not run this code but should work.
 
Share this answer
 
v2
Comments
Maciej Los 2-Mar-17 10:40am    
Graeme_Grant 2-Mar-17 10:44am    
Maciej Los 2-Mar-17 11:04am    
Didn't knew it. Good to know.
Cheers,
Maciej
Graeme_Grant 2-Mar-17 11:07am    
VS has many tricks. ;)
junisarfraz 2-Mar-17 12:13pm    
dear graeme actually we get multiple responses from client. in some response there are different nodes how we will generalize it so if there are different nodes code do not throw error..
Hi,

look at the examples on this page:

XmlDocument Class (System.Xml)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900