472,986 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 992
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Johnson | last post by:
Greetings, i want to be able to use linq new technology with sql server. the senario i am not able to do so far is as follow: 1- i program with linq 2- be able to generate and manipulate...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
4
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
1
by: shapper | last post by:
Hi, I wonder, is there some tool that transforms SQL procedures to LINQ? :-) I want to use LINQ but I have so much work done in SQL that would be great to transform my SQL code to LINQ. ...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
1
by: hrishy | last post by:
Hi Terry Oops i never realised the mistake i have commited I apologise (i thought changing the subject line would make a new thread) I apologise (I thought Python programmers were smart and...
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
4
by: George | last post by:
I am a bit conservative type and usually give some time for technology to mature before starting to try it. Today my question is Linq. To start using it or not. So here is the voting questions....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.