Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
var q = from b in okuyucu.Descendants("ARAC")
                           select new
                           {
                               userName= (string)b.Element("userName"),
                               login= (string)b.Element("login"),
                               password= (string)b.Element("password")
                           }

I write a xml document from two different windows form. And I read this xml from another windows form.
My problem is that
My first form don't write "login". When I read xml it's occurs because there is'nt any element which is name "login";
I want to write like that
if login is exist then login= (string)b.Element("login") else login=""
How can I do it in "select new{}"
Posted
Comments
Sinisa Hajnal 8-Apr-15 6:39am    
Exactly as you wrote: if b.Element("login") != null "do something" else "do something else"
amagitech 8-Apr-15 6:45am    
if(b.Element("Login")!=null){Login=(string)b.Element("login")}else{Login=""},
Error 259 'Tarayici2c_SUZ.Surregate.SigortaTipi' is a 'type' but is used like a 'variable' C:\crm\Crm2C\crm2C\Tarayici2c_SUZ\MainForm.cs 243 33 Tarayici2c_SUZ

1 solution

Use null-coalescing operator (?? operator):
C#
var q = from b in okuyucu.Descendants("ARAC")
        select new
        {
            userName = (string)b.Element("userName"),
            login = (string)b.Element("login") ?? string.Empty,
            password = (string)b.Element("password")
        };
 
Share this answer
 
Comments
amagitech 8-Apr-15 9:45am    
thank you.
Mario Z 8-Apr-15 9:52am    
you're welcome
Maciej Los 8-Apr-15 10:44am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900