Get specific xml lines with linq - c#

I have an XML looking like this:
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<Alert>warning</Alert>
</row>
</data>
I wan't to get all the rows. In this case, the value that I wan't is "Alert".
This is as fare as I've got...
using (XmlReader reader = cmd.ExecuteXmlReader())
{
string xmlFile = "";
while (reader.Read())
{
xmlFile = reader.ReadOuterXml();
}
var xmlElement = XElement.Parse(xmlFile);
var result = xmlElement.Elements("data").Where(x => x.Value.Equals("row")).ToList();
}
I know something is wrong with my linq, but I'm quite new to linq and would like some help.
Thanks!

XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
var xDocument = XDocument.Load("path");
var result = xDocument.Descendants(ns + "row").ToList();

I hope this will work. Please try this.
var xDocument = XDocument.Load(#"XmlFilePath");
//Use below if you have xml in string format
// var xDocument = XDocument.Parse("XmlString");
XNamespace ns = xDocument.Root.GetDefaultNamespace();
var result = xDocument.Descendants(ns + "row").ToList();

Related

get specific node from XML using XDOCUMENT

I have an XML document with ISO-8859-1 encoding.
I can load the XML with XDOCUMENT, but i can't storage a specific node.
Can somebody help me with this?
The xml:
<?xml version="1.0" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TERMEKADATOutput xmlns="http://xmlns.oracle.com/orawsv/PUPHAX/PUPHAXWS">
<RETURN>
<OBJTERMEKADAT>
<TTT>210037069</TTT>
<TK>OGYI-T-04617/05</TK>
</OBJTERMEKADAT>
</RETURN>
</TERMEKADATOutput>
</soap:Body>
</soap:Envelope>
And the code:
XDocument xmlDoc = null;
using (StreamReader oReader = new StreamReader(#"C:\Users\maruzsama\source\repos\EPPuphax\EPPuphax\asd.xml", Encoding.GetEncoding("ISO-8859-1")))
{
xmlDoc = XDocument.Load(oReader); //this working fine
var result = from q in xmlDoc.Descendants("OBJTERMEKADAT")
select new
{
asd = q.Element("TTT").Value
};
Console.ReadKey();
}
I need the data from the TTT node (210037069)
Try following changes :
XDocument xmlDoc = null;
using (StreamReader oReader = new StreamReader(#"C:\Users\maruzsama\source\repos\EPPuphax\EPPuphax\asd.xml", Encoding.GetEncoding("ISO-8859-1")))
{
xmlDoc = XDocument.Load(oReader); //this working fine
XElement TERMEKADATOutput = xmlDoc.Descendants().Where(x => x.Name.LocalName == "TERMEKADATOutput").First();
XNamespace ns = TERMEKADATOutput.GetDefaultNamespace();
var result = from q in TERMEKADATOutput.Descendants(ns + "OBJTERMEKADAT")
select new
{
asd = q.Element(ns + "TTT").Value
};
Console.ReadKey();
}

Unable to get value from Descendants() or Element() using XML

I can't seem to get a value from child nodes of an xml file. I feel like I have tried everything. All I want is to get the value of latitude and longitude of the location child node in the xml file. What am I doing wrong? Maybe I should try JSON instead of XML.
private void RequestCompleted(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = (HttpWebResponse)request.EndGetResponse(result);
StreamReader stream = new StreamReader(response.GetResponseStream());
try
{
XDocument xdoc = XDocument.Load(stream);
XElement root = xdoc.Root;
XNamespace ns = xdoc.Root.Name.Namespace;
List<XElement> results = xdoc.Descendants(ns + "GeocodeResponse").Descendants(ns + "result").ToList();
List<XElement> locationElement = results.Descendants(ns + "geometry").Descendants(ns + "location").ToList();
List<XElement> lat = locationElement.Descendants(ns + "lat").ToList();
List<XElement> lng = locationElement.Descendants(ns + "lng").ToList();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.Message)
}
}
xml
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>134 Gearger Circle, Lexington, KY, USA</formatted_address>
<geometry>
<location>
<lat>36.31228546</lat>
<lng>-91.4444399</lng>
</location>
<location_type>ROOFTOP</location_type>
</geometry>
<place_id>ChIJtwDV05mW-IgRyJKZ7fjmYVc</place_id>
</result>
</GeocodeResponse>
Also here is a debug value that shows a count of zero. not sure what that means. I just need the value of lat and lng.
If I understand your question correctly you are looking for the list of all the lat and lng in the XML Document.
XDocument xdc = XDocument.Load(stream);
var AllLats = xdc.Descendants("lat");
var AllLong = xdc.Descendants("lng");
You wont need to drill down to the hierarchy to get the XML Nodes value with Descendants
Another part is ns that is your namespace has to be included for the XML which looks like
<SOmeNS:address_component>
not for the elements which does have name without :
attaching the screenshot to see if you want this output.
Your code works correctly, make sure your xml loaded in the line using debugger.
XDocument xdoc = XDocument.Load(stream);
And check namespace, when executing your code i got empty string in ns or try removing the ns from your code.
XNamespace ns = xdoc.Root.Name.Namespace;
To get the lat and long use below code.
List<XElement> lat = locationElement.Descendants(ns + "lat").ToList();
List<XElement> lng = locationElement.Descendants(ns + "lng").ToList();
var latitudeval = lat[0].value;
var longitudeval = lng[0].value;
Get the element you are interested in by name. Then get the first and last child node since the first child is lat and the last child is long.
var sw = doc.Descendants("location");
var lat = sw.Descendants().First();
var lng = sw.Descendants().Last();
//XNamespace ns = xdoc.Root.Name.Namespace;
XNamespace ns = xdoc.GetDefaultNamespace();
The only way I was able to fix my issue was to send a request for JSON instead of XML. Then using Regex I was able to remove the <string> portion of the response. This worked perfectly and JSON is easier to work with in my opinion anyways.
private void RequestCompleted(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = (HttpWebResponse)request.EndGetResponse(result);
JObject jdoc = null;
Stream stream = response.GetResponseStream();
try
{
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
Regex rgx = new Regex("<.*\\>");
string newResult = rgx.Replace(text, "");
JObject json = JObject.Parse(newResult);
jdoc = json;
JArray results = (JArray)json["results"];
if (results.Count == 0)
{
}
else
{
foreach(JObject obj in results)
{
string formattedAddress = (string)obj["formatted_address"];
double lat = (double)obj["geometry"]["location"]["lat"];
double lng = (double)obj["geometry"]["location"]["lng"];
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.Message);
}
}

Can't get attribute value with C# XPath

I've spent so much time with this already, still can't get the value of NTE attribute. Can someone please help?
C#
StreamReader sr = new StreamReader(resp.GetResponseStream());
XPathDocument xmlDoc = new XPathDocument(sr); // holds xml document
XPathNavigator xmlNav = xmlDoc.CreateNavigator(); //evaluates XPath expressions
XPathNodeIterator node = xmlNav.Select("/DATA2SC/CALL");
string dne = xmlNav.GetAttribute("NTE", "");
Console.WriteLine(dne);
sr.Close();
XML
<?xml version="1.0"?>
<DATA2SC PIN="00000">
<CALL
TR_NUM="00000001"
STATUS="WAITING_FOR_APPROVAL"
NTE="$15.00">
<PROBLEM>
Text
</PROBLEM>
</CALL>
</DATA2SC>
Can you try this code please ?
I already check and it's work
StreamReader sr = new StreamReader("c:\\x.xml");
XPathDocument xmlDoc = new XPathDocument(sr); // holds xml document
XPathNavigator xmlNav = xmlDoc.CreateNavigator(); //evaluates XPath expressions
var node = xmlNav.SelectSingleNode("/DATA2SC/CALL");
string dne = node.GetAttribute("NTE", "");
Console.WriteLine(dne);
OR
XDocument docXmlWorld = XDocument.Load("c:\\x.xml");
foreach (var node1 in docXmlWorld.Descendants("DATA2SC"))
{
foreach (var node2 in node1.Descendants("CALL"))
{
string dne = node2.Attribute("NTE").Value;
Console.Out.WriteLine(dne);
}
}
Or you can do like this too:
XDocument docXmlWorld = XDocument.Load("c:\\x.xml");
//Get the first child => [DATA2SC]
XElement elementNodeDATA2SC = docXmlWorld.Element("DATA2SC");
//Get the first child => [CALL]
XElement elementNodeCALL = elementNodeDATA2SC.Element("CALL");
//Get the attribute NTE from [CALL] node
string dne = elementNodeCALL.Attribute("NTE").Value;
Console.Out.WriteLine(dne);
The Select method, returns a collection of all nodes with the specified XPath.
You can use SelectSingleNode, to select the first node.
var node = xmlNav.SelectSingleNode("/DATA2SC/CALL");
string dne = node.GetAttribute("NTE", "");

How to read xml file c#

<CPT xmlns="http://www.example.org/genericClientProfile" xmlns:ns2="http://www.blahblah.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/genericClientProfile genericClientProfile.xsd">
<header>
<serviceId>CPT-UK</serviceId>
<versionId>1.0</versionId>
<brandCode>CUK</brandCode>
<creationTime>2013-09-26T13:55:32.31+02:00</creationTime>
</header>
</CPT>
I need to be able to read the elements in the header tag. I'm struggling to read the values for some reason, which i'm not sure of. What i've tried:
public ActionResult readxmldata()
{
using (var db = new CPTEntities())
{
var file = System.IO.Directory.GetFiles("C:\\Workspace\\CPTStaging","*.xml");
foreach (var xmldoc in file)
{
XmlDocument docpath = new XmlDocument();
docpath.Load(xmldoc);
CPTPROFILE doc = new CPTPROFILE();
db.SaveChanges();
H_HEADER header = new H_HEADER();
header.SERVICEID = docpath.SelectSingleNode("//CPT/header/#serviceId").Value;
header.VERSIONID = Convert.ToDecimal(docpath.SelectSingleNode("//CPT/header/#versionId").Value);
header.CREATIONTIME = Convert.ToDateTime(docpath.SelectSingleNode("//CPT/header/#creationTime").Value);
header.BRANDCODE = docpath.SelectSingleNode("//CPT/header/#brandCode").Value;
db.CPTPROFILEs.AddObject(doc);
db.SaveChanges();
}
}
Your XML uses namespaces. xmlns attribute declares default namespace. You should use it for each element of the XML.
XNamespace ns = "http://www.example.org/genericClientProfile";
XDocument doc = XDocument.Load(xmldoc);
XElement header = doc.Root.Element(ns + "header");
For comparison, here is one way to do it with Linq-to-XML:
XDocument doc = XDocument.Load(xmlFileName);
XNamespace ns = "http://www.example.org/genericClientProfile";
var header = doc.Descendants(ns+"header").Single();
H_HEADER header = new H_HEADER();
header.SERVICEID = (string) header.Element(ns + "serviceId");
header.VERSIONID = (double) header.Element(ns + "versionId");
header.BRANDCODE = (string) header.Element(ns + "brandCode");
header.CREATIONTIME = (DateTime) header.Element(ns + "creationTime");

LINQ to XML returns no result

I am using Linq to to parse an XML, but it return no result:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<downloadInfoResponse xmlns="http://webService">
<downloadInfoReturn>
<city>city</city>
<companyName>company name</companyName>
</downloadInfoReturn>
</downloadInfoResponse>
</soapenv:Body>
</soapenv:Envelope>
Code:
public class Merc
{
public string CompanyName { get; set; }
}
using (XmlReader reader = XmlReader.Create(new StringReader(result)))
{
XDocument doc = XDocument.Load(reader, LoadOptions.SetLineInfo);
List<Merc> m = (from downloadInfoReturn in doc.Descendants("downloadInfoReturn")
select new Merc
{
CompanyName = downloadMerchantInfoReturn.Element("companyName").Value
}).ToList();
}
Is there any other good method to do it? Thank you.
Your XML file contains namespaces thus you need to specify it when perform querying:
XNamespace xn = "http://webService";
doc.Descendants(xn + "downloadInfoReturn")
Because you are missing the namespace while querying the xml, also your class name doesn't match, try the following code, it works on my side.
List<Merc> m = null;
XNamespace ns = "http://webService";
using (XmlReader reader = XmlReader.Create(new StringReader(result)))
{
XDocument doc = XDocument.Load(reader, LoadOptions.SetLineInfo);
m = (from downloadInfoReturn in doc.Descendants(ns + "downloadInfoReturn")
select new Merc
{
CompanyName = downloadInfoReturn.Element(ns+ "companyName").Value
}).ToList<Merc>();
}
Console.WriteLine(m.Count); // this will show 1

Categories