-4

For this kind of XML, How to fetch data of the tag in c#

<soapenv:Envelope xmlns:stor="http://www.jboss.org/store" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <stor:FileNexusKey>
            <stor:TransRefGUID>FN_201918926275492952</stor:TransRefGUID>
            <stor:CertificateNumber>898976</stor:CertificateNumber>
            <stor:DocumentType>Name Change Request</stor:DocumentType>
            <stor:DocumentSubType>ULNAME</stor:DocumentSubType>
            <stor:DocumentID>5190071</stor:DocumentID>
            <stor:DOB>1999-02-22</stor:DOB>
            <stor:ClientFirstName>MS</stor:ClientFirstName>
            <stor:ClientLastName>Dhoni</stor:ClientLastName>
        </stor:FileNexusKey>
    </soapenv:Body>
</soapenv:Envelope>
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
Kavy
  • 1
  • 1

1 Answers1

0

You could try to uses services converting XML to C#, such as Xml2CSharp, which in your case generates the following types:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="FileNexusKey", Namespace="http://www.jboss.org/store")]
    public class FileNexusKey 
    {
        [XmlElement(ElementName="TransRefGUID", Namespace="http://www.jboss.org/store")]
        public string TransRefGUID { get; set; }
        [XmlElement(ElementName="CertificateNumber", Namespace="http://www.jboss.org/store")]
        public string CertificateNumber { get; set; }
        [XmlElement(ElementName="DocumentType", Namespace="http://www.jboss.org/store")]
        public string DocumentType { get; set; }
        [XmlElement(ElementName="DocumentSubType", Namespace="http://www.jboss.org/store")]
        public string DocumentSubType { get; set; }
        [XmlElement(ElementName="DocumentID", Namespace="http://www.jboss.org/store")]
        public string DocumentID { get; set; }
        [XmlElement(ElementName="DOB", Namespace="http://www.jboss.org/store")]
        public string DOB { get; set; }
        [XmlElement(ElementName="ClientFirstName", Namespace="http://www.jboss.org/store")]
        public string ClientFirstName { get; set; }
        [XmlElement(ElementName="ClientLastName", Namespace="http://www.jboss.org/store")]
        public string ClientLastName { get; set; }
    }

    [XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body 
    {
        [XmlElement(ElementName="FileNexusKey", Namespace="http://www.jboss.org/store")]
        public FileNexusKey FileNexusKey { get; set; }
    }

    [XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope 
    {
        [XmlElement(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName="stor", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Stor { get; set; }
        [XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Soapenv { get; set; }
    }
}

You could then write a deserializer yourself (or use one of the many available on NuGet and the internet in general)

using System.Xml.Serialization;

public class Serializer
{       
    public T Deserialize<T>(string input) where T : class
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));

        using (StringReader reader = new StringReader(input))
        {
            return (T) serializer.Deserialize(reader);
        }
    }
}

and can now do the following:

string xml = "..."; // Your XML here.

Envelope soapEnvelope = new Serializer().Deserialize<Envelope>(xml);  
FileNexusKey fileNexusKey = soapEnvelope.Body.FileNexusKey;

to get a FileNexusKey object from the XML.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • Can we do it using LINQ,by taking actual tag names. – Kavy Jan 11 '19 at 09:15
  • @Kavy what do you mean by "taking actual tag names"? What is it that you want to do? – Thomas Flinkow Jan 11 '19 at 09:16
  • Hi Thomas, Just like we can use List to traverse through nodes by modifying the code to traverse certificate number. https://stackoverflow.com/questions/53920967/how-to-fetch-values-from-xml-and-iterate-to-store-in-variable/53921663?noredirect=1#comment94701739_53921663 – Kavy Jan 11 '19 at 10:12
  • @Kavy I see... sadly I don't know whether a framework-inbuilt way or a third-party library exists that offers requested functionality, so I cannot help you with that, sorry. – Thomas Flinkow Jan 11 '19 at 10:29