471,310 Members | 1,211 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Linq over xml question

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
Jun 27 '08 #1
2 944
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

IHi all
I>
II have a List of person objects using Generics. Each person will have
Ian Id, firstname and may have a DepartmentId
I>
II am trying to convert this list to xml. However if the person doesnt
Ihave a departmentId, I dont want to write out a departmentId
Iattribute. However I dont know how I can achieve this... I have tried
I(assuming personsDataSource1 is my list):
I>
IXElement x1 =
Inew XElement("root",
Inew XElement("persons",
Ifrom p in personsDataSource1
Iselect
Inew XElement("person",
Inew XAttribute("firstname", p.Firstname),
Inew XAttribute("Id", p.Age)
Inew XAttribute("deparementId",
Ip.DepartmentId)
I)));
II need some conditional check againt the deparementId attribute, and
Ionly write it if that conditon is true - How can I do this using Linq
Ito XML?
I>
IMany thanks
I>

Jun 27 '08 #2
Ilyas wrote:
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<Personpersons = 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.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave Johnson | last post: by
28 posts views Thread by Marc Gravell | last post: by
4 posts views Thread by BeSharp | last post: by
1 post views Thread by shapper | last post: by
9 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
14 posts views Thread by thj | last post: by
1 post views Thread by hrishy | last post: by
4 posts views Thread by CSharper | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post: by
4 posts views Thread by George | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.