Retrieve (Get) node value from XML using C# in ASP.Net

Last Reply one month ago By Mudassar

Posted one month ago

Hi Friends , 

How to get the full value of the Xml data using c#

I need to get all string in a xml format

xml string : 

 <TRMData>
 <Data>
 <CaseID>244186</CaseID>
 <Category>Prepaid Card - Request</Category>
 <CreatedDate>7/16/2020 7:46:26 AM</CreatedDate>
 <CaseOwnerECN>86022</CaseOwnerECN>
 <CaseStatus>In Progress</CaseStatus>
 <FinacleCustID>39284713</FinacleCustID>
 <FinacleMobileNumber>9782898979</FinacleMobileNumber>
 </Data>
</TRMData>

now getting output : TRMData="244186"

Expecting Output = TRMData= 

 <Data>
 <CaseID>244186</CaseID>
 <Category>Prepaid Card - Request</Category>
 <CreatedDate>7/16/2020 7:46:26 AM</CreatedDate>
 <CaseOwnerECN>86022</CaseOwnerECN>
 <CaseStatus>In Progress</CaseStatus>
 <FinacleCustID>39284713</FinacleCustID>
 <FinacleMobileNumber>9782898979</FinacleMobileNumber>
</Data>

class properties :  

public class TRMProperties
{
        [XmlRoot(ElementName = "Request")]
        public class TRMFileUpdationRequest
        {
            [XmlElement(ElementName = "Filename")]
            public string Filename { get; set; }

            [XmlElement(ElementName = "TRMData")]
            public string TRMData { get; set; }
         }
}

Web service file method  

[WebMethod()]
public string TRMFileUpdation(string RequestData)
{
XmlDocument xmldoc = new XmlDocument();
string lstrReturn = "";
StringBuilder lstrXmlString = new StringBuilder();
Models.TRM.TRMProperties.TRMFileUpdationRequest objRequest = new Models.TRM.TRMProperties.TRMFileUpdationRequest();
try
{
clsErrorLog.WriteAppLogFiles(RequestData, DateTime.Now, "TRMFileUpdation", mstrMethods, GetDeviceInfo());
RequestData = clsCommonCryptography.StringDecrypt(RequestData);

using (TextReader sr = new StringReader(RequestData))
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(ETC_WebService.Models.TRM.TRMProperties.TRMFileUpdationRequest));
objRequest = (ETC_WebService.Models.TRM.TRMProperties.TRMFileUpdationRequest)serializer.Deserialize(sr);
}
lstrReturn = mobjIMPS.TRMFileUpdation(objRequest);
lstrReturn = clsCommonCryptography.StringEncrypt(lstrReturn);
return lstrReturn;
}
catch (Exception ex)
{
clsErrorLog.WriteErrLogFiles(RequestData, "TRMFileUpdation", mstrMethods, GetDeviceInfo(), 0, ex);
lstrReturn = ETC_WebService.Models.Helper.clsGeneral.XMLStatus("Response", "017", "Please try again");
lstrReturn = clsCommonCryptography.StringEncrypt(lstrReturn);
return lstrReturn;
}
}

Request Data : 

 <TRMData>
 <Data>
 <CaseID>244186</CaseID>
 <Category>Prepaid Card - Request</Category>
 <CreatedDate>7/16/2020 7:46:26 AM</CreatedDate>
 <CaseOwnerECN>86022</CaseOwnerECN>
 <CaseStatus>In Progress</CaseStatus>
 <FinacleCustID>39284713</FinacleCustID>
 <FinacleMobileNumber>9782898979</FinacleMobileNumber>
 </Data>
</TRMData>

Kindly suggest me to i solve this solution 

Thanks & regards 

Paul.S

Posted one month ago

Using XPath it can be done.

XML

<?xml version="1.0" encoding="utf-8" ?>
<TRMData>
    <Data>
        <CaseID>244186</CaseID>
        <Category>Prepaid Card - Request</Category>
        <CreatedDate>7/16/2020 7:46:26 AM</CreatedDate>
        <CaseOwnerECN>86022</CaseOwnerECN>
        <CaseStatus>In Progress</CaseStatus>
        <FinacleCustID>39284713</FinacleCustID>
        <FinacleMobileNumber>9782898979</FinacleMobileNumber>
    </Data>
</TRMData>

Namespace

using System.Xml;

Code

protected void Page_Load(object sender, EventArgs e)
{
    //Load the XML file in XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("~/XML/Data.xml"));

    //Fetch all the Nodes.
    XmlNodeList nodeList = doc.SelectNodes("//text()");

    //Loop through the selected Nodes.
    foreach (XmlNode node in nodeList)
    {
        //Fetch the Node and Attribute values.
        Response.Write(node.InnerText);
        Response.Write("<br />");
    }
}