So I am having a hard time understanding how to use linq in C#. I found some examples but couldn't find one that would match the case I am looking for. Given the following xml:
<?xml version="1.0"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http
://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://gra
phical.weather.gov/xml/DWMLgen/schema/DWML.xsd">
<head>
<product srsName="WGS 1984" concise-name="time-series" operational-mode="off
icial">
<title>NOAA's National Weather Service Forecast Data</title>
<field>meteorological</field>
<category>forecast</category>
<creation-date refresh-frequency="PT1H">2013-09-18T07:17:35Z</creation-dat
e>
</product>
<source>
<more-information>http://graphical.weather.gov/xml/</more-information>
<production-center>Meteorological Development Laboratory<sub-center>Produc
t Generation Branch</sub-center></production-center>
<disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
<credit>http://www.weather.gov/</credit>
<credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
<feedback>http://www.weather.gov/feedback.php</feedback>
</source>
</head>
</dwml>
I want to print out the creation-date attribute value. The only way I was able to do this was by the code below:
XElement xElement = XElement.Load(XmlReader.Create(new StringReader(xml)));
var nodes = xElement.Elements("head").Elements("product").Elements("creation-date");
foreach (var attr in nodes)
{
Console.WriteLine("value = " + attr.Value);
}
I am sure there is a much better way using queries. I tried playing with the select statements but couldn't get it to work correctly without having to do some manipulation. Would be really nice to just have one query without having to loop through the result set.
String output=xElement.Descendants("creation-date")
.Select(x=>x.Value).First();
That will give you collection of parsed DateTime objects:
var dates = from cd in xdoc.Descendants("creation-date")
select (DateTime)cd;
You can enumerate them:
foreach(DateTime date in dates)
Console.WriteLine(date);
Also if it is possible that other elements besides product can contain creation-date, then you can use following XPath to select creation dates of products only:
xdoc.XPathSelectElements("dwml/head/product/creation-date")
You can merge your values using String.Join method:
Console.WriteLine(String.Join(Environment.NewLine, nodes.Select(x => (string)x)));
But to be clear, it will perform collection enumeration anyway.
Related
I have the following chunk of XML code that I can easily generate.
<?xml version="1.0" encoding="utf-8"?>
<sessions>
<session date="14.10.2016" time="0:1" amount="1">
<Folder>C:\Users</Folder>
<Folder>C:\Tes2t</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
</session>
<session date="14.10.2016" time="15:40" amount="7">
<Folder>C:\Users</Folder>
<Folder>C:\Tes2taaaa</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
</session>
</sessions>
I am searching for data with attribute time 15:40 and date 14.10.2016 using following function
private static IEnumerable<XElement> FindElements(string filename, string date, string time)
{
XElement x = XElement.Load(filename);
return x.Descendants().Where(e => e.Attributes("date").Any(a => a.Value.Equals(date)) &&
e.Attributes("time").Any(a => a.Value.Equals(time)));
}
Function being executed like:
foreach (XElement x in FindElements(pathToXml, "14.10.2016", "15:40"))
Console.WriteLine(x.ToString());
Everything is fine, but the output is
<session date="14.10.2016" time="15:40" amount="7">
<Folder>C:\Users</Folder>
<Folder>C:\Tes2taaaa</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
</session>
And I need just the folders, eg.
<Folder>C:\Users</Folder>
<Folder>C:\Tes2taaaa</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
How do I achieve this? Help please.
(It seems that I am a little bit late, but..) in some cases like this using Xpath is easier than Linq .
var folders = XDocument.Load(filename)
.XPathSelectElements("//session[#date='14.10.2016' and #time='15:40']/Folder");
You are currently returning the Element that has attribute of date and time with these values. What you should add to it is to return its child elements of Folder. You can do this by adding .Elements("Folder") after the .Where.
However, I think you can write your query a bit nicer:
Look for all the sessions that the values of those attriute equal to the given input. Then return the element.Elements("Folder").
I've added the .SelectMany() to flatten the inner list of child elements
string date = "14.10.2016";
string time = "15:40";
var result = (from element in XDocument.Load("data.xml").Descendants("session")
where element.Attribute("date")?.Value == date &&
element.Attribute("time")?.Value == time
select element.Elements("Folder")).SelectMany(item => item).ToList();
I have the following xml part and am trying to extract the value where key is known. The example below is a snippet, from a larger xml that contains 1000's of nodes.
<?xml version="1.0" encoding="utf-8"?>
<DictionarySerializer>
<item>
<key>key1</key>
<value>CONTENT1</value>
</item>
<item>
<key>key2</key>
<value>CONTENT2</value>
</item>
</DictionarySerializer>
i assume the above is a string called xml,
then with
XDocument.Parse(xml)
.Descendants("key")
.Where(x => (string)x.Value == "key1")
.FirstOrDefault().NextNode.ToString()
I can get the string <value>CONTENT1</value> But i simply cannot get my head around how to get the value of the value node to to say.
I am afrad it is super simple, and i just are stuck in a coffein loop :-)
XDocument.Parse(xml)
.Descendants("key")
.Where(x => (string)x.Value == "key1")
.FirstOrDefault().Value.ToString()
you should use .Value property instead of .NextNode
If you want to get all keys and values from the XML from all 1000 elemnts. You can use:
Dictionary<string, string> elements = new Dictionary<string, string>();
xml.Root.Elements().ToList().ForEach(xmlElement =>
{
elements.Add(xmlElement.Descendants("key").First().Value,
xmlElement.Descendants("value").First().Value);
});
So, the elements dictionary will contain all of your 1000 nodes.
Try to cast NextNode to XElement and get Value from it.
Considering you can use XPath expressions.
expression = #"//Item[Key='1']/Value"
XmlNodeList nodeList = xmlDocument.SelectNodes(expression);
This would give you the value node(s) of items with Key=1. Just find the value of the desired node.
I believe using XDocument you can also try,
string output = xDocument.XPathEvaluate(expression);
I trying to figure out a solution for a tiny problem I have, but I didn't have much success in last two days. I've went through many posts here but I failed to find an adequate solution for myself. I know it's probably basic question for you guys, but it seems like I've lost focus and can think clearly about this issue.
So, this is the issue. I have a sample XML below, with the structure I need. What I want is to read this XML, go to the event I want and than read/ store all test elements.
<Config>
<Events>
<Event_1>
<NameOfEvent>SomeName:</NameOfEvent>
<test id="test001">
<xpath>"some xpath"</xpath>
<value>someValue1</value>
<tagName>none</tagName>
</test>
<test id="test002">
<xpath>"some xpath"</xpath>
<value>someValue2</value>
<tagName>none1</tagName>
</test>
<test id="test003">
<xpath>"some xpath"</xpath>
<value>someValue3</value>
<tagName>none2</tagName>
</test>
</Event_1>
</Events>
</Config>
What I`ve done so far is this:
string EventCode="Event_1";
var doc= XDocument.Load(#"C:\new\test\testConfig.xml");
var result = from y in doc.Descendants(EventCode).
Where(y =>(string)y.Element("path").Attribute("id")=="test001"
{
NameOfEvent = y.Element("NameOfEvent").Value,
xpath= y.Element("test").Element("xpath").Value
value =y.Element("test").Element("value").Value,
tag =y.Element("test").Element("tagName").Value
};
Than, I want to use foreach loop, to access all test elements. But, I`m only able to access chiled nodes of first element test.
Can you help with this? Thanks a lot in advance!!!
Assuming your XML has no specific namespaces
var evt = (from el in doc.Descendants("test")
where el.Parent.Name == "Event_1"
group el by el.Parent.Element("NameOfEvent").Value into g
select new {
Name = g.Key,
Tests = g.Select(x => new {
XPath = x.Element("xpath").Value,
Value = x.Element("value").Value,
TagName = x.Element("tagName").Value
})
}).FirstOrDefault();
Console.WriteLine("Event name: " + evt.Name);
foreach (var test in evt.Tests)
{
Console.WriteLine(test.XPath);
Console.WriteLine(test.Value);
Console.WriteLine(test.TagName);
}
Live demo
This is my XML:
<?xml version="1.0"?>
<formatlist>
<format>
<formatName>WHC format</formatName>
<delCol>ID</delCol>
<delCol>CDRID</delCol>
<delCol>TGIN</delCol>
<delCol>IPIn</delCol>
<delCol>TGOUT</delCol>
<delCol>IPOut</delCol>
<srcNum>SRCNum</srcNum>
<distNum>DSTNum</distNum>
<connectTime>ConnectTime</connectTime>
<duration>Duration</duration>
</format>
<format>
<formatName existCombineCol="1">Umobile format</formatName> //this format
<delCol>billing_operator</delCol>
<hideCol>event_start_date</hideCol>
<hideCol>event_start_time</hideCol>
<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss"> //node i want
<name>ConnectdateTimeAFcombine</name>
<combineDate>event_start_date</combineDate>
<combineTime>event_start_time</combineTime>
</afCombineName>
<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss"> //node i want
<name>aaa</name>
<combineDate>bbb</combineDate>
<combineTime>ccc</combineTime>
</afCombineName>
<modifyPerfixCol action="add" perfix="60">bnum</modifyPerfixCol>
<srcNum>anum</srcNum>
<distNum>bnum</distNum>
<connectTime>ConnectdateTimeAFcombine</connectTime>
<duration>event_duration</duration>
</format>
</formatlist>
I want to find format with Umobile format then iterate over those two nodes.
<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss"> //node i want
<name>ConnectdateTimeAFcombine</name>
<combineDate>event_start_date</combineDate>
<combineTime>event_start_time</combineTime>
</afCombineName>
<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss"> //node i want
<name>aaa</name>
<combineDate>bbb</combineDate>
<combineTime>ccc</combineTime>
</afCombineName>
and list all the two node's child nodes. The result should like this:
ConnectdateTimeAFcombine,event_start_date,event_start_time.
aaa,bbb,ccc
How can I do this?
foreach(var children in format.Descendants())
{
//Do something with the child nodes of format.
}
For all XML related traversing, you should get used to using XPath expressions. It is very useful. Even if you could perhaps do something easier in your specific case, it is good practice to use XPath. This way, if your scheme changes at some point, you just update your XPath expression and your code will be up and running.
For a complete example, you can have a look at this article.
You can use the System.Xml namespace APIs along with System.Xml.XPath namespace API. Here is a quick algorithm that will help you do your task:
Fetch the text node containing the string Umobile format using the below XPATH:
XmlNode umobileFormatNameNode = document.SelectSingleNode("//formatName[text()='Umobile format']");
Now the parent of umobileFormatNameNode will be the node that you are interested in:
XmlNode formatNode = umobileFormatNameNode.ParentNode;
Now get the children for this node:
XmlNodeList afCombineFormatNodes = formatNode.SelectNodes("afCombineName");
You can now process the list of afCombineFormatNodes
for(XmlNode xmlNode in afCombineNameFormtNodes)
{
//process nodes
}
This way you can access those elements:
var doc = System.Xml.Linq.XDocument.Load("PATH TO YOUR XML FILE");
var result = doc.Descendants("format")
.Where(x => (string)x.Element("formatName") == "Umobile format")
.Select(x => x.Element("afCombineName"));
Then you can iterate the result this way:
foreach (var item in result)
{
string format = item.Attribute("format").Value.ToString();
string name = item.Element("name").Value.ToString();
string combineDate = item.Element("combineDate").Value.ToString();
string combineTime = item.Element("combineTime").Value.ToString();
}
Say I call XElement.Parse() with the following XML string:
var xml = XElement.Parse(#"
<?xml version="1.0" encoding="UTF-8"?>
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<ID>7c75442509c41100b6a413b88b523bd6f46554cdbee5b6cbe27bc08cb3f6a865</ID>
<DisplayName>me</DisplayName>
</Owner>
<AccessControlList>
<Grant>
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
...
");
When it comes time to query the element, I'm forced to use fully-qualified element names because that XML document contains an xmlns attribute in its root. This requires cumbersome creations of XName instances:
var AWS_XMLNS = "http://s3.amazonaws.com/doc/2006-03-01/";
var ownerElement = xml.Element(XName.Get("AccessControlPolicy", AWS_XMLNS)).Element(XName.Get("Owner", AWS_XMLNS));
When what I really want is simply,
var ownerElement = xml.Element("AccessControlPolicy").Element("Owner");
Is there a way to make LINQ to XML assume a specific namespace so I don't have to keep specifying it?
You could simplify by using
XNamespace ns = "http://s3.amazonaws.com/doc/2006-03-01/";
var ownerElement = xml.Element(ns + "AccessControlPolicy").Element(ns + "Owner");
I don't think you can (see Jon Skeet's comment), but there are a few tricks you can do.
1) create an extension method that appends the XNamespace to your string
2) Use VB?!?