Linq to xml in c# - c#

Scenario:
Grid view gets populated in WPF window.
Having a static list in code behind.(which i want to get from a xml file).
Trying to move the static list into an xml file.For that i created a ml file in the following format
<customers>
<customer Name="abc"/>
<customer Name="def"/>
</customers>
CodeBehind:
Xdocument doc=Xdocument.load("customers.xml");
var customerList = (from e in doc.Descendants("Cusomters")
select new
{
CustomerName = e.Attribute("Name").Value
}).ToList();
I am unable to get the customer names from the xml file to the customerList.I would appreciate if someone can help me to move forward.

"Cusomters" is spelled incorrectly, should be "Customers".
Obviously this is not the code your are using since it doesn't even compile. It should be this:
XDocument doc = XDocument.Load( "customers.xml" );
var customerList = (from e in doc.Descendants( "customer" )
select new
{
CustomerName = e.Attribute( "Name" ).Value
}).ToList();
You really should mention the fact that it won't compile. That, or you copied it in by hand incorrectly, which doesn't help us help you either.
The logical problem here is that you are asking for all Customers tags, note the s at the end. You really want to look for Customer tags, which have a name attribute. Customer*s* is simply the top level group.

Use customer instead of Cusomters (XML is case-sensitive):
from e in doc.Descendants("customer")

You most likely want a List<string> so you don't need to project to an anonymous class - also there is a typo in your query ("Cusomters"):
var customerList = (from e in doc.Descendants("Customer")
select e.Attribute("Name").Value).ToList();
or with extension method syntax:
var customerList = doc.Descendants("Customer")
.Select( e => e.Attribute("Name").Value)
.ToList();

Ive always used :
doc.root.elements("Customer")
for small snippets like this.

Related

XDocument parsing

i have created a custom XDocument in c# and it looks like the following
<Filters datetimegenerated="28.07.2013 23:12PM">
<SimpleStringFilter column="xxx" table="yyy" groupby="True" seperatereport="true">
good,bad,ugly
</SimpleStringFilter>
<NumaricalFilter column="zzz" table = "mmm">zzz = 100 or zzz= 50</NumaricalFilter>
</Filters>
parsing it with in c# doesn't seem to work here is my code when i try to parse the StringFilterTags, however i get zero count from the above sample
var filters = from simplestringfilter in xdoc.Root.Element("Filters").Elements("SimpleStringFilter")
let column = simplestringfilter.Attribute("column")
let table = simplestringfilter.Attribute("table")
let groupby = simplestringfilter.Attribute("groupby")
let seperatecolumnby = simplestringfilter.Attribute("seperatereport")
let filterstringval = simplestringfilter.Value
select new
{
Column = column,
Table = table,
GroupBy = groupby,
SeperateColumnBy = seperatecolumnby,
Filterstring = filterstringval
};
what am i doing wrong?
Your query is searching off of the root element checking to see if it has a child Filters element. Since the root is the Filters element, that obviously fails which is why you are not getting any results.
There are two ways to resolve this problem. Just don't search for the Filters off of the root and your query should be fine.
var filters =
from simplestringfilter in xdoc.Root.Elements("SimpleStringFilter")
...
A better way to write it IMHO would be to not query off of the root but the document itself. It will look more natural.
var filters =
from simplestringfilter in xdoc.Element("Filters")
.Elements("SimpleStringFilter")
...

Reading a deep level xml tag with C#

I need some help using Linq to xml, I've been reading online articles but still no luck, can anyone please help me out?
I just need to read an xml file, the problem I'm having is that it has lots of sub levels and I haven't been able to access them.
<Dias>
<Dia id="0">
<Restricciones>
<Restriccion tipo="Ambiental" horaInicio="6" horaFin="10">
<Placas>
<Placa>4</Placa>
</Placas>
</Restriccion>
</Restricciones>
</Dia>
</Dias>
My current code is:
var dia = (int)DateTime.Now.DayOfWeek;
var xElement = XElement.Load("Bogota.xml");
var d = (from dias in xElement.Descendants("Dia")
where dias.Attribute("id").Value == dia.ToString()
select dias).First();
var rest = (from r in d.Descendants("Restricciones")
select r);
But I've tried several variations, but no luck so far
Can someone help?
This should work
var d = (from s in myXel.Descendants("Dia")
where s.Attribute("id").Value == dia.ToString()
select s).FirstOrDefault();
var rest = d.Descendants("Restriccion").ToList();

Orderby datetime query in a foreach loop

Hi I have a rest service which has a list of students each student has a datetime attached to there creation.
On my client side I want to order by this datetime from newest first, on my client side the code looks like this:
public FindStudent()
{
InitializeComponent();
string uriGroups = "http://localhost:8000/Service/Student";
XDocument xDoc = XDocument.Load(uriGroups);
And it lists students by first created, I thought for a moment I could have added in an orderby query into the foreach var node but I dont think that will work, is there anyway I could do this?
You can use Linq to XML as follows
var temp = from feed in xDoc.Descendants("Students")
orderby Convert.ToDateTime(feed.Element("CreatDate").Value) descending
select new Student
{
ID = Convert.ToInt32(feed.Element("ID").Value),
//rest of the properties
};
in order to sort something based on date and time you need to convert it to date and time in your for-each loop you just loop through the pure text taken from xml document it not going to get ordered since it text an but you can create a student class with varaibles Studentid, FirstName LastName" TimeAdded TimeAdded loop through the document again write every information to the object student add every object to the list sort the list the way you would like and then I think it should display in order that you want.
XDocument xDoc = XDocument.Load(uriGroups);
var sortedXdoc = xDoc.Descendants("Student")
.OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));

How to Search Within a XMl file based on Attributes values...

I am using an xml file from this link..
http://www.goalserve.com/samples/soccer_livescore.xml
..
Lets say "category" is our "Tournament" then
I need to search and show the ---
1. The listing of all the "Tournaments" in gridview or datalist.
2. The listing of matches within a selected "Tournament"..
3. The listing of events within the matches etc..
Pls guide me how to achieve this... M using a Dataset.Readxml but then the inner linking of fields become very complex...
Pls guide...
Thanks..n..regards,
The simplest way to do this is with LINQ to XML. Something like this:
var doc = XDocument.Load(url);
var tournaments = doc.Root
.Elements("category")
.Where(x => (string) x.Attribute("name") == "Tournament")
.Single(); // Is there only one matching catgeory?
var matches = tournaments
.Elements("match")
.Select(m => new
{
LocalTeam = (string) m.Element("localteam").Attribute("name"),
VisitorTeam = (string) m.Element("localteam").Attribute("name"),
Events = m.Elements("Events")
.Select(e => new
{
Player = (string) e.Attribute("player"),
Type = (string) e.Attribute("type"),
// etc
})
.ToList();
});
How you display that is then up to you. You may want to create your own "normal" types for Event, Match etc rather than using the anonymous types above.
LINQ to XML is by far the simplest way of working with XML that I've used.

Linq to Xml to Datagridview

Right, starting to go crazy here. I have the following code:
var query = (from c in db.Descendants("Customer")
select c.Elements());
dgvEditCusts.DataSource = query.ToList();
In this, db relates to an XDocument.Load call. How can I get the data into the DataGridView?
Just thought I should mention: it returns a completely blank dgv.
Not that the XML should matter too much, but here's an example:
<Root>
<Customer>
<CustomerNumber>1</CustomerNumber>
<EntryDate>2010-04-13T21:59:46.4642+01:00</EntryDate>
<Name>Customer Name</Name>
<Address>Address</Address>
<PostCode1>AB1</PostCode1>
<PostCode2>2XY</PostCode2>
<TelephoneNumber>0123456789</TelephoneNumber>
<MobileNumber>0123456789</MobileNumber>
<AlternativeNumber></AlternativeNumber>
<EmailAddress>email#address.com</EmailAddress>
</Customer>
</Root>
When you call db.Descendants("Customer") you are returning ONLY the elements that have the name Customer in it. NOT it's children. See MSDN Docs.
So when you call c.Elements() it is trying to get the Customer's child elements which don't exist b/c they were filtered out.
I think it might work if you drop the Customer filtering.
Ah, never mind, I've worked out the answer to my own question eventually. Here's the code for anyone else that may have this problem:
var query = from c in db.Descendants("Customer")
select new
{
CustomerNumber = Convert.ToInt32((string)c.Element("CustomerNumber").Value),
Name = (string)c.Element("Name").Value,
Address = (string)c.Element("Address").Value,
Postcode = (string)c.Element("PostCode1").Value + " " + c.Element("PostCode2").Value
};
dgvEditCusts.DataSource = query.ToList();

Categories

Resources