Unable to read specific fields from XML string in C#? - 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 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);

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

Related

Unable to get list from xml using xPathNavigator

List<string> list = new List<string>();
foreach (XPathNavigator node in nav.Select("configuration/company/work/worktime"))
{
string day = getAttribute(node, "day");
string time = getAttribute(node, "time");
string worktype = ?? // how to get worktype attribute valuefrom parent node
list.Add(day,time,worktype); // add to list
}
</configuration>
<company>
<work worktype="homeWork">
<worktime day="30" time="10:28"></worktime>
<worktime day="25" time="10:50"></worktime>
</work>
<work worktype="officeWork">
<worktime day="12" time="09:28"></worktime>
<worktime day="15" time="12:28"></worktime>
</work>
</company>
</configuration>
need output as :
list[0] = homeWork,30,10:28
list[1] = homeWork,25,10:50
list[2] = officeWork,12,09:28
list[3] = officeWork,15,12:28
I am trying to get the list from XML but failed to get output like given above (using xpath navigator, how can I access parent node to get worktype attribute, and other remaining inner node attribute?
I'd suggest using LINQ to XML over XPath, but if you must use XPathNavigator then you need to iterate each work element followed by each of its worktime child elements. This way you can use the worktype from the parent context:
foreach (XPathNavigator work in nav.Select("configuration/company/work"))
{
var workType = work.GetAttribute("worktype", string.Empty);
foreach (XPathNavigator worktime in work.Select("worktime"))
{
var day = worktime.GetAttribute("day", string.Empty);
var time = worktime.GetAttribute("time", string.Empty);
list.Add($"{workType}, {day}, {time}");
}
}
See this fiddle for a working demo.
Use a nested loop. Initially retrieve the work nodes with configuration/company/work. Retrieve the worktype attribute and store in a variable. Then loop through the child worktype nodes and add a string to the list for each one
Use Net Library enhanced xml (linq xml)
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 results = doc.Descendants("work").Select(x => new {
worktype = (string)x.Attribute("worktype"),
worktime = x.Elements("worktime").Select(y => new {
day = (int)y.Attribute("day"),
time = (DateTime)y.Attribute("time")
}).ToList()
}).ToList();
}
}
}

How to get the name of the child node in xml

My XML looks like this
<Location>
<AChau>
<ACity>
<EHouse/>
<FHouse/>
<GHouse/>
</ACity>
<BCity>
<HHouse/>
<IHouse/>
<JHouse/>
<KHouse/>
</BCity>
</AChau>
</Location>
I find a number of ways, I am here to find the closest answer
Get All node name in xml in silverlight
But it reads all the descendants, I need is from "Location" get "AChau"
From "Location/AChau" get "ACity" "BCity"
From "Location/AChau/ACity" get "EHouse" "FHouse" "GHouse"
How can I read only child node?
Assuming that you have an XElement, you can extract the array of names of its children using the following code:
string[] names = xElem.Elements().Select(e => e.Name.LocalName).ToArray();
For example, this code with your XML:
public static MyXExtensions
{
public static string[] ChildrenNames(this XElement xElem)
{
return xElem.Elements().Select(e => e.Name.LocalName).ToArray();
}
}
string[] names1 = xDoc.Root.ChildrenNames();
string[] names2 = xDoc.Root.Element("AChau").ChildrenNames();
string[] names3 = xDoc.XPathSelectElement("Location/AChau/ACity").ChildrenNames();
will return the following arrays respectively:
["AChau"]
["ACity", "BCity"]
["EHouse", "FHouse", "GHouse"]
If you're using XElement to get your data from xml - then all you need is FirstNode property and Elements method.
FirstNode returns first child node of element and Elements returns all direct child nodes of element.
This works if you always want the first node name under the root:
string xml = #"<Location>
<AChau>
<ACity>
<EHouse/>
<FHouse/>
<GHouse/>
</ACity>
<BCity>
<HHouse/>
<IHouse/>
<JHouse/>
<KHouse/>
</BCity>
</AChau>
</Location>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode root = doc.DocumentElement;
XmlNode first = root.FirstChild;
Try this 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
{
static void Main(string[] args)
{
string xml =
"<Location>" +
"<AChau>" +
"<ACity>" +
"<EHouse/>" +
"<FHouse/>" +
"<GHouse/>" +
"</ACity>" +
"<BCity>" +
"<HHouse/>" +
"<IHouse/>" +
"<JHouse/>" +
"<KHouse/>" +
"</BCity>" +
"</AChau>" +
"</Location>";
XElement location = XElement.Parse(xml);
var results = location.Descendants("AChau").Elements().Select(x => new
{
city = x.Name.LocalName,
houses = string.Join(",",x.Elements().Select(y => y.Name.LocalName).ToArray())
}).ToList();
}
}
}
​

Linq to XML select node from config.xml (Lambda expression or classic query)

I am using .net 4.0 winforms. In my application a have config file (config.xml), on this file i have lots nodes and child nodes, all are different, i want to select specific node and nodes inside the selected node.
I tried lots of solutions but not succeed.
Thanks in advance for your help.
This should give you the correct result:-
XDocument doc = XDocument.Load(#"XMLFilePath");
XNamespace ns = "http://schemas.datacontract.org/2004/07/Silvio.Settings";
var result = doc.Root.Element(ns + "maintenance_anomalies")
.Descendants(ns + "nom_operation")
.Select(x =>
new
{
NomOperation = (string)x,
statutList = x.Parent.Element(ns + "statuts")
.Elements(ns + "statut")
.Select(z => (string)z).ToList()
}).ToList();
Approach:
From the Xdocument object select the root node which is Main. From this select the Element maintenance_anomalies by including the Namespace associated with it. From there you can select all the descendants of nom_operation and fetch it's value. To find all statut inside nom_operation go back to parent node which is operation and from there select all statut elements.
You can also project a Type instead of anonymous type.
Getting following output:-
I had a couple of issues with your xml. First there is an invalid character so instead of using the Load method. There is also a namespace issue so I used Where method to get the tag maintenance_anomalies.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication53
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string file = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(file);
XElement maintenance_anomalies = doc.Descendants().Where(x => x.Name.LocalName == "maintenance_anomalies").FirstOrDefault();
XNamespace ns = maintenance_anomalies.Name.Namespace;
var results = maintenance_anomalies.Elements(ns + "operation").Select(x => new{
nom_operation = x.Element(ns + "nom_operation").Value,
statut = string.Join(",",x.Descendants(ns + "statut").Select(y => y.Value).ToArray())
}).ToList();
}
}
}
The line below should let u access the node u requested. Use this is your class using System.Xml;
XmlDocument XmlDocObj = new XmlDocument();
XmlNode UserNameNode = XmlDocObj.SelectSingleNode("maintenance_anomalies");

How to Linq2Xml a webservice?

I'm calling a WebService with HttpWebRequest.Create method and read it with StreamReader, (below method do this job):
private string ReadWebMethod(string address)
{
var myRequest = (HttpWebRequest)HttpWebRequest.Create(address);
myRequest.Method = "POST";
using (var responseReader = new StreamReader(myRequest.GetResponse().GetResponseStream()))
return responseReader.ReadToEnd();
}
This method works well and output a string like this:
<ArrayOfAppObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<AppObject>
<Name>MyApp</Name>
<Image>StoreApp.png</Image>
<Version>SM2.1.0</Version>
</AppObject>
</ArrayOfAppObject>
Now I want to have a look in this string, So I use XElemnet and parse the string. (below code):
XElement x = XElement.Parse(ReadWebMethod(address));
Now, When I foreach, x.Elements("AppObject"), it doesnt return any item and skip the foreach.
My foreach is like this:
foreach (var item in x.Elements("AppObject"))
{
listApplication.Add(new AppObject { Image = item.Element("Image").Value, Name = item.Element("Name").Value, Version = item.Element("Version").Value });
}
I create a local string and remove attributes after "ArrayOfAppObject" (xmlns:xsi="htt...)(some where name it xmlnamespace) and test it again, And it works as well and foreach does not skipped!
SO, How can I use the xml with namespace?
use XDocument class
using System.Xml.Linq;
//...
var xml = ReadWebMethod(address);
var xdocument = System.Xml.Linq.XDocument.Parse(xml);
updates
as your XML data define the namespace.. xmlns="http://tempuri.org/"
You must declare full XName with valid namespace. to access each element value
XName theElementName = XName.Get("AppObject", "http://tempuri.org/");
//or alternate way..
XName theElementName = XName.Get("{http://tempuri.org/}AppObject");
here 's sample test method
[TestMethod]
public void ParseXmlElement()
{
XDocument xdoc = XDocument.Parse(this.mockXml);
XName appTag = XName.Get("{http://tempuri.org/}AppObject");
XName nameTag = XName.Get("{http://tempuri.org/}Name");
XName imageTag = XName.Get("{http://tempuri.org/}Image");
XElement objElement = xdoc.Root.Element(appTag);
Assert.IsNotNull(objElement, "<AppObject> not found");
Assert.AreEqual("{http://tempuri.org/}AppObject", objElement.Name);
XElement nameElement = objElement.Element(nameTag);
Assert.IsNotNull(nameElement, "<Name> not found");
Assert.AreEqual("MyApp", nameElement.Value);
XElement imageElement = objElement.Element(imageTag);
Assert.IsNotNull(imageElement, "<Image> not found");
Assert.AreEqual("StoreApp.png", imageElement.Value);
}
using Xml.Linq this way..
[TestMethod]
public void ParseXmlLinq()
{
XDocument xdoc = XDocument.Parse(this.mockXml);
XElement app = xdoc.Root.Elements()
.FirstOrDefault(e => e.Name == XName.Get("AppObject", "http://tempuri.org/"));
Assert.IsNotNull(app, "<AppObject> not found");
XElement img = app.Elements()
.FirstOrDefault(x => x.Name == XName.Get("Image", "http://tempuri.org/"));
Assert.IsNotNull(img, "<Image> not found");
Assert.AreEqual("StoreApp.png", img.Value);
}
Note that I just mock up and parse string from your XML.

How to get an element that has : in its name?

I need to get the CountryName from this XML: http://api.hostip.info/?ip=12.215.42.19
The response XML is:
<HostipLookupResultSet version="1.0.1"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
<gml:description>This is the Hostip Lookup
Service</gml:description>
<gml:name>hostip</gml:name>
<gml:boundedBy>
<gml:Null>inapplicable</gml:Null>
</gml:boundedBy>
<gml:featureMember>
<Hostip>
<ip>12.215.42.19</ip>
<gml:name>Sugar Grove, IL</gml:name>
<countryName>UNITED STATES</countryName>
<countryAbbrev>US</countryAbbrev>
<!-- Co-ordinates are available as lng,lat -->
<ipLocation>
<gml:pointProperty>
<gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
<gml:coordinates>-88.4588,41.7696</gml:coordinates>
</gml:Point>
</gml:pointProperty>
</ipLocation>
</Hostip>
</gml:featureMember>
</HostipLookupResultSet>
Problem is I can't include : in the Descendants method because it throws:
XmlException: The ':' chracater,
hexadecimal value 0x3A, cannot be
included in a name.
Thanks
try this
var descendants = from i in XDocument.Load(xml).Descendants("Hostip")
select i.Element("countryName");
Update
complete code for downloading the xml and finding the name of countryName
string xml;
using(var web = new WebClient())
{
xml = web.DownloadString("http://api.hostip.info/?ip=12.215.42.19");
}
var descendants = from i in XDocument.Parse(xml).Descendants("Hostip")
select i.Element("countryName");
A small example on how to apply namespaces in LINQ to XML:
XElement doc = XElement.Load("test.xml");
XNamespace ns = "http://www.opengis.net/gml";
var firstName = doc.Descendants(ns + "name").First().Value;
You need to reference the gml namespace; once you've done that you should be able to navigate using the tag names that appear to the right of "gml:"
UPDATE
I'm not sure what context you're applying this to, but here's a sample console app that works:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace LinqToXmlSample
{
class Program
{
static void Main(string[] args)
{
XElement x = XElement.Load("http://api.hostip.info/?ip=12.215.42.19");
foreach (XElement hostip in x.Descendants("Hostip"))
{
string country = Convert.ToString(hostip.Element("countryName").Value);
Console.WriteLine(country);
}
Console.ReadLine();
}
}
}
var gml = (XNamespace)"http://www.opengis.net/gml";
var doc = XDocument.Load(...) or XDocument.Parse(...);
var name = doc.Descendants(gml + "featureMember").Descendants("countryName").First().Value;
Or you could go brute force and strip all the namespaces:
void RemoveNamespace(XDocument xdoc)
{
foreach (XElement e in xdoc.Root.DescendantsAndSelf())
{
if (e.Name.Namespace != XNamespace.None)
{
e.Name = XNamespace.None.GetName(e.Name.LocalName);
}
if (e.Attributes().Any(a => a.IsNamespaceDeclaration || a.Name.Namespace != XNamespace.None))
{
e.ReplaceAttributes(e.Attributes().Select(a => a.IsNamespaceDeclaration ? null : a.Name.Namespace != XNamespace.None ? new XAttribute(XNamespace.None.GetName(a.Name.LocalName), a.Value) : a));
}
}
}

Categories

Resources