Linq over xml question

I

Ilyas

Hi all

I have a List of person objects using Generics. Each person will have
an Id, firstname and may have a DepartmentId

I am trying to convert this list to xml. However if the person doesnt
have a departmentId, I dont want to write out a departmentId
attribute. However I dont know how I can achieve this... I have tried
(assuming personsDataSource1 is my list):

XElement x1 =
new XElement("root",
new XElement("persons",
from p in personsDataSource1
select
new XElement("person",
new XAttribute("firstname", p.Firstname),
new XAttribute("Id", p.Age)
new XAttribute("deparementId", p.DepartmentId)
)));

I need some conditional check againt the deparementId attribute, and
only write it if that conditon is true - How can I do this using Linq
to XML?

Many thanks
 
A

Alex Meleta

Hi Ilyas,

Then use 'where' conditions for it:
.... from p in personsDataSource1
where p.DepartmentId == null
select new XElement("person", .... or?

ps. the example above is for case when the 'doesn't have a departmentId'
means having null.
Regards, Alex
[TechBlog] http://devkids.blogspot.com

I> Hi all
I>
I> I have a List of person objects using Generics. Each person will have
I> an Id, firstname and may have a DepartmentId
I>
I> I am trying to convert this list to xml. However if the person doesnt
I> have a departmentId, I dont want to write out a departmentId
I> attribute. However I dont know how I can achieve this... I have tried
I> (assuming personsDataSource1 is my list):
I>
I> XElement x1 =
I> new XElement("root",
I> new XElement("persons",
I> from p in personsDataSource1
I> select
I> new XElement("person",
I> new XAttribute("firstname", p.Firstname),
I> new XAttribute("Id", p.Age)
I> new XAttribute("deparementId",
I> p.DepartmentId)
I> )));
I> I need some conditional check againt the deparementId attribute, and
I> only write it if that conditon is true - How can I do this using Linq
I> to XML?
I>
I> Many thanks
I>
 
M

Martin Honnen

Ilyas said:
I need some conditional check againt the deparementId attribute, and
only write it if that conditon is true - How can I do this using Linq
to XML?

Can you show us the type of p.DepartmentId and tell us exactly which
value (null?) the property has when you say a person does not have a
department id?

If the department id is a reference type and is null when you say a
person does not have an id then you can simply check
person.Id != null ? new XAttribute("id", person.Id) : null
e.g. in this example

List<Person> persons = new List<Person>() { new Person() {
Name = "Foo", Id = "P1" }, new Person() { Name = "Bar" } };
XDocument personDoc =
new XDocument(
new XElement("persons",
from p in persons
select new XElement("person",
new XAttribute("name", p.Name),
p.Id != null ? new XAttribute("id", p.Id) :
null)));
personDoc.Save(Console.Out);

the resulting XML is

<persons>
<person name="Foo" id="P1" />
<person name="Bar" />
</persons>

The class Person looks like this:

class Person
{
public string Name { get; set; }
public string Id { get; set; }
}

so Id is of type string and a reference type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top