How to loop though XElements and compare values using C# - c#

I have an XElement which contains similar nodes but different values.
Like my XElement look like this
<Information>
<Emp>
<A.EMPLID>1</A.EMPLID>
<A.Phone>12##</A.Phone>
</Emp>
<Emp_Add>
<B.ID>125</B.ID>
<Add>XXXXXXX</Add>
</Emp_Add>
<Emp_Add>
<B.ID>1256</B.ID>
<Add>ZZZZZZ</Add>
</Emp_Add>
</Information>
Actually I need to go through each <Emp_Add> node - pick up the value of <B.ID>
and compare it with <Emp>.<A.EMPLID> value. If values are same then display message "Values matched" else message "Values does not match" using C# code.
How to use for each loop and compare for such XElement.

I think you need something like this
See my dotnetfiddle.
var xml =
"<Information>\r\n\t<Emp>\r\n\t\t<A.EMPLID>1</A.EMPLID>\r\n\t\t<A.Phone>12##</A.Phone>\r\n\t</Emp>\r\n\t<Emp_Add>\r\n\t\t<B.ID>125</B.ID>\r\n\t\t<Add>XXXXXXX</Add>\r\n\t</Emp_Add>\r\n\t<Emp_Add>\r\n\t\t<B.ID>1256</B.ID>\r\n\t\t<Add>ZZZZZZ</Add>\r\n\t</Emp_Add>\r\n</Information>";
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XDocument xmlObj = XDocument.Parse(xmlDoc.OuterXml);
var emplId = xmlObj.Descendants().Descendants().Descendants().FirstOrDefault().Value;
var empsAdd = xmlObj.Descendants().Descendants().Where(d => d.Name.LocalName == "Emp_Add");
foreach (var emp in empsAdd)
{
var currentEmpIdNode = emp.Descendants().FirstOrDefault();
Console.WriteLine(currentEmpIdNode != null && currentEmpIdNode.Value == emplId
? "Values matched"
: "Values does not match");
}

Related

How do I select the element's string of descendant?

How do I select tag "lat" and grab the value "123" base on the "eventid" I want? For example, I want to select <lle:lat>123</lle:lat> of <lle:eventid>ID1</lle:eventid> without using XPath, just LINQ to XML only
Expected output:
123
Below is the xml file:
<soapenv:Letter xmlns:soapenv="http://www.w3.org/2001/12/soap-envelope" soapenv:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
<soapenv:Body xmlns:lle="http://www.aab.org/lifelogevents" >
<lle:Event>
<lle:eventid>ID1</lle:eventid>
<lle:tweet>
<lle:text>This is some tweet in my day</lle:text>
<lle:location>
<lle:lat>123</lle:lat>
<lle:long>456</lle:long>
</lle:location>
</lle:tweet>
</lle:Event>
<lle:Event>
<lle:eventid>ID2</lle:eventid>
<lle:instagram-post-update>
<lle:text>This is some status update in my day</lle:text>
<lle:location>
<lle:lat>789</lle:lat>
<lle:long>987</lle:long>
</lle:location>
</lle:instagram-post-update>
</lle:Event>
</soapenv:Body>
</soapenv:Letter>
And this is my C# code so far:
XDocument xmldoc = XDocument.Load(#"C:\Users\JACK\source\repos\LINQ_Learn\LINQ_Learn\xmlFile.xml");
XNamespace lle = "http://www.aab.org/lifelogevents";
XNamespace soapenv = "http://www.w3.org/2001/12/soap-envelope";
var lati = from data in xmldoc.Descendants(nlle + "Event")
where (string)data.Element(nlle + "eventid") == "ID1"
select data.Element(nlle + "lat").Value;
foreach(var lat in lati)
{
Console.WriteLine(lat);
}
Here's a solution using Linq to navigate the xml instead. First we get all the descendant "Events", then filter them by the defined "eventid" value and then return their "lat" nodes. FirstOrDefault() returns the first match or the null value if none were found. Finally, the ? symbol serves to return the value of the match only if it isn't null, otherwise it would throw an exception.
var lat = xmldoc.Descendants(lle + "Event")
.Where(x => x.Element(lle + "eventid").Value == "ID1")
.Descendants(lle + "lat")
.FirstOrDefault()?.Value;

Linq statement in C# to extract data from XElement

I have a List containing elements like this:
{<d:element m:type="SP.KeyValue" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<d:Key>Path</d:Key>
<d:Value>https://my.home.site.com</d:Value>
<d:ValueType>Edm.String</d:ValueType>
</d:element>}
I'd like help to discern the Linq statement required to extract only the "https://my.home.site.com" values from said List<>. The catch here is that we cannot only use the <d:Value> because only XElements in this list that has a <d:Key> value of Path, like in the example above, actually contain URLs in the <d:Value> key.
Does anyone know the magic Linq statement that would perform said data extract?
Assuming your data is coming from an XML file similar to this:
<?xml version="1.0"?>
<root xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<d:element m:type="SP.KeyValue">
<d:Key>Path</d:Key>
<d:Value>https://my.home.site.com</d:Value>
<d:ValueType>Edm.String</d:ValueType>
</d:element>
<d:element m:type="SP.KeyValue">
<d:Key>NotPath</d:Key>
<d:Value>https://my.home.site.com</d:Value>
<d:ValueType>Edm.String</d:ValueType>
</d:element>
</root>
The following code:
XElement root = XElement.Load("Some file");
List<string> urls;
//Query Syntax
urls = (from e in root.Elements(d + "element")
where e.Element(d + "Key").Value == "Path"
select e.Element(d + "Value").Value);
//Or
//Method Syntax
urls = (from e in root.Elements(d + "element")
where e.Element(d + "Key").Value == "Path"
select e.Element(d + "Value").Value).ToList();
Console.WriteLine(string.Join(",", urls));
Will result in (note that it ignores the "NotPath" key):
https://my.home.site.com
You can check out a live example here and check out this for more XElement information.
if you actually have a List of XElement:
var list = new List<XElement>(); //however you get your XElement collection
var values = list.Where(x => x.Elements().First(e => e.Name.LocalName == "Key").Value == "Path")
.Select(x => x.Elements().First(e => e.Name.LocalName == "Value").Value)
if you have an XDocument, you'd just modify the beginning of the query slightly.
I think that problem if with naespace declaration. Try this:
string xml = "<d:element m:type=\"SP.KeyValue\" xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\">"+
"<d:Key>Path</d:Key>"+
"<d:Value>https://my.home.site.com</d:Value>"+
"<d:ValueType>Edm.String</d:ValueType>"+
"</d:element>";
XDocument xmlObj = XDocument.Parse(xml);
XNamespace ns_d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
var result = xmlObj.Descendants(ns_d + "Value").Select(x => x.Value);

Extract the XML value in to string c#

I have the string which has the XML tag like
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lab:lab xmlns:udf="http://ghjhjhj.com/ri/userdefined"
xmlns:ri="http://kjkj.com/ri"
xmlns:lab="http://iuiuu.com/ri/lab"
uri="https://hhjjhjhj.org/api/v2/labs/1">
<name>Administrative Lab</name>
<billing-address>
<street></street>
<city></city>
<state></state>
<country></country>
<postalCode></postalCode>
<institution></institution>
<department></department>
</billing-address>
<shipping-address>
<street></street>
<city></city>
<state></state>
<country></country>
<postalCode></postalCode>
<institution></institution>
<department></department>
</shipping-address>
<udf:field type="String" name="Account ID">adm</udf:field>
<website></website>
</lab:lab>"
In order to just extract the value adm i.e any value between the <udf> tag should I be using the XDocument or the XmlDocument.I understand I can use XDocument.Parse but this I am not sure how to give the tag name. I tried below
XDocument new_doc = XDocument.Parse(new_responseString);
var a = from udf in new_doc.Descendants("udf") select udf.Value;
But there can be additional udf fields in future so what will I be checking should be name="Account ID" and I am not sure how to do this
How can I retrieve this?
You can use Attribute method for retrieving attribute's value of XElement.
var udf = "http://ghjhjhj.com/ri/userdefined";
var new_doc = XDocument.Parse(new_responseString);
var fieldValues = doc.Descendants(udf + "field")
.Where(field => field.Attribute("name").Value.Equals("Account ID"))
.Select(field => field.Value);
foreach (var value in fieldValues)
{
Console.WriteLine(value);
}
If you need only one value then use FirstOrDefault method
var fieldValue =
doc.Descendants(udf + "field")
.FirstOrDefault(field => field.Attribute("name").Value.Equals("Account ID"))
.Value;
But be aware - this query will throw exception of there no elements with attribute name = "Account ID"
Probably this might do the trick for you
XNamespace laburi = "http://iuiuu.com/ri/lab";
XNamespace udfuri = "http://ghjhjhj.com/ri/userdefined";
XDocument xdoc = XDocument.Load("some.txt");
var a = xdoc.Elements(laburi + "lab").Elements(udfuri + "field").FirstOrDefault().Value;

C# XML How to retrive innerText of field by attribute?

Here is the XML sample:
<?xml version="1.0" ?>
<XMLScreen xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CX>80</CX>
<CY>24</CY>
<Formatted>true</Formatted>
<Field>
<Location position="1" left="1" top="0" length="69" />
<Attributes Base="226" Protected="false" FieldType="High" />
*SDC SCHEDULING CATEGORY UPDATE
</Field>
</XMLScreen>
I want to retrive the Inner text of each field based on its Location position.
What I have so far is:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(myEm.CurrentScreenXML.GetXMLText());
XmlNodeList fields = xmlDoc.GetElementsByTagName("Field");
MessageBox.Show("Field spot: " + i + " Contains: " + fields[i].InnerText);
And I want to be able to just extract the inner text of the field by passing in a number of the location position. So if I say foo[i] I want to be able to get the innertext
*SDC SCHEDULING CATEGORY UPDATE
You should use a xpath search query :
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
int nodeId = 4;
XmlNode node = xmlDoc.SelectSingleNode(String.Format(#"//Location[#position='{0}']", nodeId));
if (node != null)
{
String field = node.ParentNode.InnerText;
}
Something like that, with XDocument instead of XmlDocument (well, if you're not in .net 3.5 or higher, we'll have a problem).
private string GetTextByLocationId(XDocument document, int id)
{
var field = document.Descendants("Field").FirstOrDefault(m => m.Element("Location").Attribute("position").Value == id.ToString());
if (field == null) return null;
return field.Value;
}
and usage
var xDocument = XDocument.Load(<pathToXmlFile or XmlReader or string or ...>);
var result = GetTextByLocationId(xDocument, 1);
EDIT
or if you want a dictionary with :key = position / value = text
private static Dictionary<int, string> ParseLocationAndText(XDocument document)
{
var fields = document.Descendants("Field");
return fields.ToDictionary(
f => Convert.ToInt32(f.Element("Location").Attribute("position").Value),
f => f.Value);
}
Try,
XElement root = XElement.Parse(myEm.CurrentScreenXML.GetXMLText());
XElement field = root.XPathSelectElement(
string.Format("Field[Location/#position='{0}']", 1));
string text = field.Value;
You will need to use the following using to use XPath with XElements.
using System.Xml.XPath;

How can I get the href attribute value out of an <?xml-stylesheet> node?

We are getting an XML document from a vendor that we need to perform an XSL transform on using their stylesheet so that we can convert the resulting HTML to a PDF. The actual stylesheet is referenced in an href attribute of the ?xml-stylesheet definition in the XML document. Is there any way that I can get that URL out using C#? I don't trust the vendor not to change the URL and obviously don't want to hardcode it.
The start of the XML file with the full ?xml-stylesheet element looks like this:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.fakeurl.com/StyleSheet.xsl"?>
As a processing instruction can have any contents it formally does not have any attributes. But if you know there are "pseudo" attributes, like in the case of an xml-stylesheet processing instruction, then you can of course use the value of the processing instruction to construct the markup of a single element and parse that with the XML parser:
XmlDocument doc = new XmlDocument();
doc.Load(#"file.xml");
XmlNode pi = doc.SelectSingleNode("processing-instruction('xml-stylesheet')");
if (pi != null)
{
XmlElement piEl = (XmlElement)doc.ReadNode(XmlReader.Create(new StringReader("<pi " + pi.Value + "/>")));
string href = piEl.GetAttribute("href");
Console.WriteLine(href);
}
else
{
Console.WriteLine("No pi found.");
}
Linq to xml code:
XDocument xDoc = ...;
var cssUrlQuery = from node in xDoc.Nodes()
where node.NodeType == XmlNodeType.ProcessingInstruction
select Regex.Match(((XProcessingInstruction)node).Data, "href=\"(?<url>.*?)\"").Groups["url"].Value;
or linq to objects
var cssUrls = (from XmlNode childNode in doc.ChildNodes
where childNode.NodeType == XmlNodeType.ProcessingInstruction && childNode.Name == "xml-stylesheet"
select (XmlProcessingInstruction) childNode
into procNode select Regex.Match(procNode.Data, "href=\"(?<url>.*?)\"").Groups["url"].Value).ToList();
xDoc.XPathSelectElement() will not work since it for some reasone cannot cast an XElement to XProcessingInstruction.
You can also use XPath. Given an XmlDocument loaded with your source:
XmlProcessingInstruction instruction = doc.SelectSingleNode("//processing-instruction(\"xml-stylesheet\")") as XmlProcessingInstruction;
if (instruction != null) {
Console.WriteLine(instruction.InnerText);
}
Then just parse InnerText with Regex.
To find the value using a proper XML parser you could write something like this:
using(var xr = XmlReader.Create(input))
{
while(xr.Read())
{
if(xr.NodeType == XmlNodeType.ProcessingInstruction && xr.Name == "xml-stylesheet")
{
string s = xr.Value;
int i = s.IndexOf("href=\"") + 6;
s = s.Substring(i, s.IndexOf('\"', i) - i);
Console.WriteLine(s);
break;
}
}
}
private string _GetTemplateUrl(XDocument formXmlData)
{
var infopathInstruction = (XProcessingInstruction)formXmlData.Nodes().First(node => node.NodeType == XmlNodeType.ProcessingInstruction && ((XProcessingInstruction)node).Target == "mso-infoPathSolution");
var instructionValueAsDoc = XDocument.Parse("<n " + infopathInstruction.Data + " />");
return instructionValueAsDoc.Root.Attribute("href").Value;
}
XmlProcessingInstruction stylesheet = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;

Categories

Resources