locked
retrieving values from XML in c# RRS feed

  • Question

  • User-309557751 posted

    hi

    When i call the page with a query string, it returns an xml document with data that matches the query string.

    for eg

    <VerifyOutput MerchantID="" MerchantTxnID="" AMT="" VERIFIED="NODATA" BID="null" bankname="null" txnId="null" discriminator="null" surcharge="null" CardNumber="null"TxnDate="null"/>

    How can i retrieve values of MerchantID , MerchantTxnID , AMT ,VERIFIED,bankname  and other details from xml in my webpage?

    Can anyone please help

    Monday, May 9, 2016 9:24 AM

Answers

  • User36583972 posted

    Hi Neethus,

    I have made a sample on my side. It is working. You can refer the following code in my project. Make some adjustments and integrated into your project.

              //example 
                string xml = @"<VerifyOutput MerchantID="""" MerchantTxnID="""" AMT="""" VERIFIED=""NODATA"" BID=""null"" bankname=""null"" txnId=""null""  discriminator=""null""  surcharge=""null"" CardNumber=""null"" TxnDate=""null"" /> ";
    
                XDocument xdoc = new XDocument();
                xdoc = XDocument.Parse(xml);// load xml string
                //xdoc = XDocument.Load("url");//load xml file url
                serid.InnerHtml = xml;
                xdoc.Save(Server.MapPath("~/Uploads/test.xml")); //save the post html tag
    
                //root
                XElement rootelement = xdoc.Root;
                Response.Write(rootelement.Attributes("MerchantID").First().Value);
                Response.Write(" <br />  ");
                Response.Write(rootelement.Attributes("VERIFIED").First().Value);
                Response.Write(" <br />  ");
                Response.Write(rootelement.Attributes("txnId").First().Value);
    
                //Nodes
                foreach (XElement element in xdoc.Root.Nodes())
                {
                    foreach (XElement elementn in element.Nodes())
                    {
                        //Response.Write(elementn + " <br />  ");
                    }
                }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2016 2:42 AM

All replies

  • User-821857111 posted

    You can use the XElement.Load method to load the fragment of XML that you showed and then use Linq to XML to query it. Here's some examples: https://msdn.microsoft.com/en-us/library/mt693115.aspx

    Monday, May 9, 2016 12:27 PM
  • User36583972 posted

    Hi Neethus,

    I have made a sample on my side. It is working. You can refer the following code in my project. Make some adjustments and integrated into your project.

              //example 
                string xml = @"<VerifyOutput MerchantID="""" MerchantTxnID="""" AMT="""" VERIFIED=""NODATA"" BID=""null"" bankname=""null"" txnId=""null""  discriminator=""null""  surcharge=""null"" CardNumber=""null"" TxnDate=""null"" /> ";
    
                XDocument xdoc = new XDocument();
                xdoc = XDocument.Parse(xml);// load xml string
                //xdoc = XDocument.Load("url");//load xml file url
                serid.InnerHtml = xml;
                xdoc.Save(Server.MapPath("~/Uploads/test.xml")); //save the post html tag
    
                //root
                XElement rootelement = xdoc.Root;
                Response.Write(rootelement.Attributes("MerchantID").First().Value);
                Response.Write(" <br />  ");
                Response.Write(rootelement.Attributes("VERIFIED").First().Value);
                Response.Write(" <br />  ");
                Response.Write(rootelement.Attributes("txnId").First().Value);
    
                //Nodes
                foreach (XElement element in xdoc.Root.Nodes())
                {
                    foreach (XElement elementn in element.Nodes())
                    {
                        //Response.Write(elementn + " <br />  ");
                    }
                }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2016 2:42 AM