1

I am trying to extract values of : url,ttype,tempTnxId,token,txnStage from the following XML string:

 <?xml version="1.0" encoding="UTF-8"?>
    <MMP>
       <MERCHANT>
          <RESPONSE>
             <url>https://payment.xyz.com/paynetz/epi/fts</url>
             <param name="ttype">abc</param>
             <param name="tempTxnId">12319507</param>
             <param name="token">x5H9RrhgfXvamaqEl6GpY4uCoXHN%2FlEm%2BUpaaKuMQus%3D</param>
             <param name="txnStage">1</param>
          </RESPONSE>
       </MERCHANT>
    </MMP>

So far I have only been able to extract values with index using following code:

 foreach (XmlNode node in doc.SelectNodes("/MMP/MERCHANT/RESPONSE/param"))
 {   
   string tempTxnId= doc.SelectNodes("/MMP/MERCHANT/RESPONSE/param")[1].InnerText;//only works with index and not name
 }

/MMP/MERCHANT/RESPONSE/param or /MMP/MERCHANT/RESPONSE/ttype does not return anything.

This solution :Getting specified Node values from XML document does not seem to be working for me.

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstring);
var result = doc.Elements("table"); ///cant find Elements, Element is is not identified by the compiler 
3
  • Cannot reproduce. Getting 4 XmlElements with "/MMP/MERCHANT/RESPONSE/param". Commented Nov 30, 2017 at 6:20
  • 2
    Have you tried using XDocument with Linq to Xml? Commented Nov 30, 2017 at 6:22
  • @SaniSinghHuttunen I think I might have missed something. I am getting 4 elements as well. But I can access values using index only doc.SelectNodes("/MMP/MERCHANT/RESPONSE/param")[1].InnerText; and not name Commented Nov 30, 2017 at 6:29

3 Answers 3

0

You commented that you cannot select by name ttype.
ttype is a value and not a name.
The element name is param.
The single attribute name of the element param is name.

If you need to get the InnerText of the element param with attribute name equal to ttype (or the other values) then you could do something like:

var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><MMP><MERCHANT><RESPONSE><url>https://payment.xyz.com/paynetz/epi/fts</url><param name=\"ttype\">abc</param><param name=\"tempTxnId\">12319507</param><param name=\"token\">x5H9RrhgfXvamaqEl6GpY4uCoXHN%2FlEm%2BUpaaKuMQus%3D</param><param name=\"txnStage\">1</param></RESPONSE></MERCHANT></MMP>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

// This gets 4 nodes.
var paramNodes = doc.SelectNodes("/MMP/MERCHANT/RESPONSE/param");
foreach (XmlElement e in paramNodes)
{
  Console.WriteLine(e.Attributes[0].Value + "=" + e.InnerText);
}

// These each get a single node.
var ttypeNode = doc.SelectSingleNode("/MMP/MERCHANT/RESPONSE/param[@name=\"ttype\"]");
var tempTxnIdNode = doc.SelectSingleNode("/MMP/MERCHANT/RESPONSE/param[@name=\"tempTxnId\"]");
var tokenNode = doc.SelectSingleNode("/MMP/MERCHANT/RESPONSE/param[@name=\"token\"]");
var txnStageNode = doc.SelectSingleNode("/MMP/MERCHANT/RESPONSE/param[@name=\"txnStage\"]");

Console.WriteLine(ttypeNode.InnerText);
Console.WriteLine(tempTxnIdNode.InnerText);
Console.WriteLine(tokenNode.InnerText);
Console.WriteLine(txnStageNode.InnerText);
Sign up to request clarification or add additional context in comments.

Comments

0

You can select node by attribute value like this (assuming this is what you are trying to do):

        doc.SelectNodes("/MMP/MERCHANT/RESPONSE/param[@name='ttype']")
            .Cast<XmlNode>().ToList()
            .ForEach(x=>Console.WriteLine(x.InnerText));

Comments

0

Using xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var response = doc.Descendants("RESPONSE").Select(x => new {
                url = (string)x.Element("url"),
                ttype = x.Elements().Where(y => (string)y.Attribute("name") == "ttype").Select(z => (string)z).FirstOrDefault(),
                tempTxnId = x.Elements().Where(y => (string)y.Attribute("name") == "tempTxnId").Select(z => (string)z).FirstOrDefault(),
                token = x.Elements().Where(y => (string)y.Attribute("name") == "token").Select(z => (string)z).FirstOrDefault(),
                txnStage = x.Elements().Where(y => (string)y.Attribute("name") == "txnStage").Select(z => (int)z).FirstOrDefault()
            }).FirstOrDefault();


        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.