简体   繁体   中英

Unable to read specific fields from XML string in C#?

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 

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));

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();


        }
    }
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM