473,813 Members | 3,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 personsDataSour ce1 is my list):

XElement x1 =
new XElement("root" ,
new XElement("perso ns",
from p in personsDataSour ce1
select
new XElement("perso n",
new XAttribute("fir stname", p.Firstname),
new XAttribute("Id" , p.Age)
new XAttribute("dep arementId", 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 1008
Hi Ilyas,

Then use 'where' conditions for it:
.... from p in personsDataSour ce1
where p.DepartmentId == null
select new XElement("perso n", .... 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 personsDataSour ce1 is my list):
I>
IXElement x1 =
Inew XElement("root" ,
Inew XElement("perso ns",
Ifrom p in personsDataSour ce1
Iselect
Inew XElement("perso n",
Inew XAttribute("fir stname", p.Firstname),
Inew XAttribute("Id" , p.Age)
Inew XAttribute("dep arementId",
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<Personpers ons = new List<Person>() { new Person() {
Name = "Foo", Id = "P1" }, new Person() { Name = "Bar" } };
XDocument personDoc =
new XDocument(
new XElement("perso ns",
from p in persons
select new XElement("perso n",
new XAttribute("nam e", 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
1899
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 xml files 3- all this xml files are stored in Sqlserver
28
16436
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 write a safe but easy implementation (i.e. no codedom) for an IBindingListView.Filter (by compiling to a Predicate<T>). Anybody know if this is possible at all? Marc
4
549
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.). Assume I want to select rows from a database and check whether a specific column contains keywords from a list of keywords. The following works just fine: List<stringsearchTerms = new List<string>() { "Maria", "Pedro" };
1
1869
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. Thanks, Miguel
9
2506
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 about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
14
3693
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
225
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 they did know what LINQ was) I don't apologise ( i dont apologise for the third one not sounding cocky here but i did google but nothing much came up thats when i posted the question here since i always see Python programmers are somehow smarter...
4
5103
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
12440
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: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
4
2537
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. 1. It speeds up development. Yes or No? 2. It makes programs easier to code and read. Yes or No? 3. Perfomance is the same (or comparable) comparing Linq with MsSql and ADO.NET
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10408
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10426
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10141
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.