linq query for descendants of n.element - c#

Hi can anyone help with a linq query, normally I populate my data grid like so from my client side for a GET request to the webservice:
{
string uri = "http://localhost:8002/Service/Customer";
XDocument xDoc = XDocument.Load(uri);
var customer = xDoc.Descendants("Customer")
.Select(n => new
{
CustomerID = n.Element("CustomerID").Value,
Firstname = n.Element("FirstName").Value,
Surname = n.Element("LastName").Value,
Age = n.Element("Age").Value,
//Time = DateTime.Parse(n.Element("TimeAdded").Value)
})
.ToList();
dataGrid1.ItemsSource = customer;
}
Which works fine but now I have linked customers to hire dates and my xml looks like so :
<ArrayOfCustomer>
<Customer>
<CustomerID>1</CustomerID>
<FirstName>G</FirstName>
<LastName>Graam</LastName>
<Age>27</Age>
<CustomerHireDate>
<HireDate>
<HireFromDate>15.07.2012</HireFromDate>
<HireToDate>29.07.2012</HireToDate>
</HireDate>
</CustomerHireDate>
</Customer>
</ArrayOfCustomer>
So far im stuck at the below method of trying to populate a datagrid with descendants of descendants:
string uriShowCarHires = "http://localhost:8002/Service/Customer/{anything}";
string Uri = uriShowCarHires.Replace("{anything}", textBox1.Text);
XDocument xDoc = XDocument.Load(Uri);
foreach (var node in xDoc.Descendants("Customer"))
{
\\..... how do you get the descendants of descendants for each n.element?
}
Im not sure if this will even populate a datagrid the way im thinking, i was hoping to avoid getting an "array" inside one of the datagrids cells. Im looking for an output abit like this:
Name etc | HireFromDate | HireToDate
G 09.12.2012 01.01.2013
If anyone could help would be grateful thanks

I think you are looking for something like this (it isn't the most effecient, but it should work):
var customer = xDoc.Descendants("Customer")
.Select(n => new
{
Firstname = n.Element("FirstName").Value,
Surname = n.Element("LastName").Value,
HireFromDate = n.Element("CustomerHireDate")
.Descendents("HireDate").First()
.Descendents("HireFromDate").First().Value,
HireFromDate = n.Element("CustomerHireDate")
.Descendents("HireDate").First()
.Descendents("HireToDate").First().Value
})
.ToList();

Related

C# deserialize xml using linq

I have the following xml file
<?xml version="1.0" encoding="utf-8"?>
<Launchpad>
<Shortcuts>
<Shortcut Id="1">
<Type>Folder</Type>
<FullPath>C:\bla\bla\bla</FullPath>
<Name>Proximity</Name>
</Shortcut>
<Shortcut Id="2">
<Type>Folder</Type>
<FullPath>C:\bla</FullPath>
<Name>Visual Studio 2017</Name>
</Shortcut>
</Shortcuts>
</Launchpad>
I am trying to deserialize to an object like this: (Tried query syntax first, didn't work either)
XDocument xd = XDocument.Load(FullPath);
// query syntax
//var shortcuts = (from s in xd.Descendants("Shortcuts")
// select new Shortcut()
// {
// Id = Convert.ToInt32(s.Attribute("Id")),
// TypeOfLink = GetTypeFromString(s.Descendants("Type")
// .First()
// .Value),
// FullPathToTarget = s.Descendants("FullPath")
// .First()
// .Value,
// Name = s.Descendants("Name").First().Value,
// }).ToList();
// method syntax
List<Shortcut> shortcuts = xd.Descendants("Shortcuts")
.Select(s =>
new Shortcut()
{
//Id = Convert.ToInt32(s.Attribute("Id")),
TypeOfLink = GetTypeFromString(s.Descendants("Type")
.First().Value),
FullPathToTarget = s.Descendants("FullPath")
.First().Value,
Name = s.Descendants("Name")
.First().Value,
}).ToList();
return shortcuts;
For some reason I only get a single shortcut object in the list. Also, for some reason, the s.Attribute("Id") is null.
If anyone has any suggestions to improve the linq query or why the attribute is not working it would be a great help
You have to read .Descendants("Shortcut") instead of .Descendants("Shortcuts").
Something like this:
List<Shortcut> shortcuts = xd.Descendants("Shortcut").Select(s =>
new Shortcut()
{
Id = s.Attribute("Id").Value,
TypeOfLink = s.Descendants("Type").First().Value,
FullPathToTarget = s.Descendants("FullPath").First().Value,
Name = s.Descendants("Name").First().Value,
}).ToList();
I get the full list by selecting the Shortcut as a descendant of Shortcuts.
Also, ID is an XAttribute so you cant convert it to int.
You need to use Value to get the attributes value.
XDocument xd = XDocument.Load(ms);
XElement root = xd.Document.Root;
var list = root.Descendants("Shortcuts").Descendants("Shortcut").Select(x =>
new Shortcut()
{
Id = Convert.ToInt32(x.Attribute("Id").Value),
TypeOfLink = GetTypeFromString(x.Descendants("Type")
.First().Value),
FullPathToTarget = x.Descendants("FullPath")
.First().Value,
Name = x.Descendants("Name").First().Value
}).ToList();

C# reading xml inside certain node

I need help. I'm trying to figure out a way to read inside a bracket like this one:
<group id = "56">
<name>Counter</name>
</group>
In the code, there are mulitiple places where the same pattern comes back, and I would like to get all the group id number's and their name.
This is my code:
XDocument doc = XDocument.Parse(_XmlFile);
var results = doc.Descendants("group").Select(x => new
{
id = (int)x.Attribute("id"),
name = x.Attribute("name").Value,
}).ToList();
Console.WriteLine(results);
Thanks
Your code looks quite OK, but name is an element and not an attribute, so it should be
XDocument doc = XDocument.Parse(_XmlFile);
var results = doc.Descendants("group").Select(x => new
{
id = (int)x.Attribute("id"),
name = (string)x.Element("name"),
}).ToList();
foreach (var x in results)
Console.WriteLine("id: {0} name: {1}", x.id, x.name);
Use GetElementsByTagName method.
Here is the microsoft article explaining it with examples.
https://msdn.microsoft.com/en-us/library/dc0c9ekk(v=vs.110).aspx
"Name" is not an attribute, but a child node. The solution is something like this:
XDocument doc = XDocument.Parse(_XmlFile);
var results = doc.Descendants("group").Select(x => new
{
id = int.Parse(x.Attribute("id").Value),
name = x.Descendants("name").First().Value
}).ToList();

XML parsing issue in WinStoreApp

i just stuck in an issue when ever i parse my XML like this using Xdocument :
XDocument xmldoc = XDocument.Load(datafromxml);
var data = from query in xmldoc.Descendants("Chapter")
select new MyEntityclass
{
Sampledata = (string)query.Element("SubChapter")
};
i got only one tag inner value from this. i.e from the first tag value only. remaining are skipped.
My XML is like :
<Chapter>
<SubChapter ChapterID="1"><![CDATA["Some data here 1"]]></SubChapter>
<SubChapter ChapterID="2"><![CDATA["Some data here 2"]]></SubChapter>
<SubChapter ChapterID="3"><![CDATA["Some data here 3"]]></SubChapter>
</Chapter>
when i checked in a debug i just got the value of "chapterid : 1". please help me to sort out this. thanks
Your query now only retreives (iterates) the outer node.
You need something like (untested)
var data = from query in xmldoc.Descendants("Chapter")
from chapter in query.Elements("SubChapter") // note the 's'
select new MyEntityclass
{
Sampledata = (string)chapter
};
var data = xmlDoc.Root
.Elements("SubChapter")
.Select(x => new MyEntityclass { Sampledata = (string)x });

How to get multiple elements with same name in a node with XDocument?

I have a xml with this strucure:
<news>
<id><![CDATA[1]]></id>
<title><![CDATA[My title]]></title>
<date><![CDATA[17-06-2013]]></date>
<machine><![CDATA[a]]></machine>
<machine><![CDATA[b]]></machine>
<machine><![CDATA[c]]></machine>
<machine><![CDATA[d]]></machine>
</news>
<news>
<id><![CDATA[2]]></id>
<title><![CDATA[My title 2]]></title>
<date><![CDATA[17-06-2013]]></date>
<machine><![CDATA[a]]></machine>
<machine><![CDATA[b]]></machine>
<machine><![CDATA[c]]></machine>
<machine><![CDATA[d]]></machine>
</news>
and I read it like this:
var datas = from query in loadedData.Descendants("news")
select new News
{
Title = (string)query.Element("title"),
Id = (string)query.Element("id"),
StrDate = (string)query.Element("date"),
list = query.Elements("machine")
};
the code
list = query.Elements("machine")
doesn't work. How to get a list with the elements with tags "machine"
The below mentioned code should work out. I have considered list as an object of List
var datas = from query in loadedData.Descendants("news")
select new News
{
Title = (string)query.Element("title"),
Id = (string)query.Element("id"),
StrDate = (string)query.Element("date"),
list = (from xele in query.Descendants("machine")
select xele.Value).ToList<string>();
};

c# code for getting xml elements

I have following xml file:
<os:tax>
<os:cat name="abc" id="1">
<os:subcat name="bcd" id="11">
<os:t name="def" id="111">
<os:cut name="hello" id="161" cutURL="/abc/a.html"/>
<os:cut name="hello2" id="162" cutURL="/abc1/a1.html"/>
<os:cut name="hello3" id="163" cutURL="/abc4/a3.html"/>
</os:t>
</os:subcat>
</os:cat>
<os:cat name="def" id="2">
<os:subcat name="bcd" id="22">
<os:t name="def" id="222">
<os:cut name="hello" id="171" cutURL="/abcs/a.html"/>
<os:cut name="hello2" id="172" cutURL="/abcs1/a1.html"/>
<os:cut name="hello3" id="173" cutURL="/abcs4/a3.html"/>
</os:t>
</os:subcat>
</os:cat>
</os:tax>
Its a bigger file with lot of os:cat under it. I need to get string value for:
os:cat -> id , name
os:subcat -> id, name
os: t -> id, name
os: cut -> id, name, cutURL
I have this so far:
XmlNodeList tax = xmlDoc.GetElementsByTagName("os:tax");
foreach (XmlNode node in tax)
{
XmlElement cat = (XmlElement)node;
// than get string values here?
}
Is this correct? Can anyone show me efficient way to do this? Or right way to do this easily?
Here's a sample for LINQ to XML - but I strongly suggest you look for full LINQ to XML tutorials. (And get to grips with the rest of LINQ...)
(EDIT: I hadn't spotted the t part before.)
XDocument doc = XDocument.Load("tax.xml");
XNamespace os = "http://something"; // You haven't included the declaration...
foreach (XElement cat in doc.Descendants(os + "cat"))
{
int catId = (int) cat.Attribute("id");
string catName = (string) cat.Attribute("name");
foreach (XElement subcat in cat.Elements(os + "subcat"))
{
int subId = (int) subcat.Attribute("id");
string subName = (string) subcat.Attribute("name");
foreach (XElement t in subcat.Elements(os + "t"))
{
int tId = (int) t.Attribute("id");
string tName = (string) t.Attribute("name");
foreach (XElement cut in t.Elements(os + "cut"))
{
string cutId = (int) cut.Attribute("id");
string cutName = (string) cut.Attribute("name");
string cutUrl = (string) cut.Attribute("cutURL");
// Use the variables here
}
}
}
}
This assumes there's only one subcat for each cat - I don't know if that's correct.
You may want to express this as a LINQ query instead... it depends on what you need to do.
Here's a LINQ query version - having looked at everything you're using, I think this makes more sense:
XDocument doc = XDocument.Load("tax.xml");
XNamespace os = "http://something"; // You haven't included the declaration...
var query = from cat in doc.Descendants(os + "cat")
from subcat in cat.Elements(os + "subcat")
from t in subcat.Elements(os + "t")
from cut in t.Elements(os + "cut")
select new
{
CatId = (int) cat.Attribute("id"),
CatName = (string) cat.Attribute("name"),
SubCatId = (int) subcat.Attribute("id"),
SubCatName = (string) subcat.Attribute("name"),
TId = (int) t.Attribute("id"),
TName = (string) t.Attribute("name"),
CutId = (int) cut.Attribute("id")
CutName = (string) cut.Attribute("name")
CutUrl = (string) cut.Attribute("cutURL")
};
Note that I've converted all the ID values to int rather than string. You could convert them to strings instead, of course, but if they are all integers, it makes sense to parse them as such.
Jon's suggestion to use LINQ to XML is the way to go, but I've included the old way below. My XPath is a little (very) rusty, so forgive me if there are any mistakes:
var doc = new XmlDocument(); //your document
var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("os", "http://bla");
foreach (XmlNode node in doc.SelectNodes("//os:subcat/os:t/os:cut", xmlnsManager))
{
string value = node.Attributes.GetNamedItem("name").Value;
}
See this article if you need more help: http://support.microsoft.com/kb/318545
Consider using XElement along with Lambda expression.
XNamespace osNs = "http://xml.com"; // Like Jon said, you haven't included the namespace url
XElement taxElement = XElement.Load("path/to/your/xml/file");
foreach(var cat in taxElement.Decendents(osNs + "cat"))
{
Console.WriteLine(cat.Attribute("id").Value);
foreach(var subcat in cat.Decendents(osNs + "subcat"))
{
Console.WriteLine(subcat.Attribute("id").Value);
foreach(var t in subcat.Decendents(osNs + "t"))
{
Console.WriteLine(t.Attribute("id").Value);
foreach(var cut in t.Decendents(osNs + "cut"))
{
Console.WriteLine(cut.Attribute("id").Value);
Console.WriteLine(cut.Attribute("name").Value);
Console.WriteLine(cut.Attribute("cutURL").Value);
}
}
}
}
It's just capturing one node at by another. If you want to get all the curURL then you can just write something like this:
foreach(var cut in taxElement.Decendents(osNs + "cut"))
{
Console.WriteLine(cut.Attribute("cutURL"));
}
Even you can use Lambda like if you want something like all os:cut where os:subcat id = 22
taxElement.Decendents("osNs + "subcat").Where(p => p.Attribute("id").Value == "22").Decendents(osNs + "cut");
Please go through some tutorial on LINQ to XML or something on XElement.
Hope this helps!

Categories