Solved

skip XElement if the value is empty

Posted on 2010-08-17
8
1,015 Views
Last Modified: 2013-12-17
I want to skip the middleInitials XElement if the value is null......is there any way I could do in the for loop mentioned below.
XElement xmlElement = new XElement(nameSpace + State.StateNewEmployeeRegistry);
            foreach (Employee employee in lstEmployee)
            {
                xmlElement.Add(new XElement(nameSpace + State.Employee, new XElement(nameSpace + Emp.EmployeeFirstName, employee.EmployeeFirstName),
                                new XElement(nameSpace + Emp.EmployeeMiddleName, employee.EmployeeMiddleName),
                                new XElement(nameSpace + Emp.EmployeeLastName, employee.EmployeeLastName),
                                new XElement(nameSpace + State.Address,
                                new XElement(nameSpace + State.UsAddress,
                                new XElement(nameSpace + Emp.EmployeeAddress1, employee.EmployeeAddress1),
                                new XElement(nameSpace + Emp.EmployeeAddress2, employee.EmployeeAddress2),
                                new XElement(nameSpace + Emp.EmployeeCity, employee.EmployeeCity),
                                new XElement(nameSpace + Emp.EmployeeState, employee.EmployeeState),
                                new XElement(nameSpace + Emp.EmployeeZipCode, employee.EmployeeZipCode))),
                                new XElement(nameSpace + Emp.EmployeeSsn, employee.Ssn)));
                xmlElement.Add(new XElement(nameSpace + Emp.EmployeeHireDate, employee.HireDate));

            }

Open in new window

0
Comment
Question by:prashant-Vanam
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:rbgCODE
ID: 33458179
Why not just check within the linq query if it not isnothing(xelemnent) then skip.
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33458322
I'm not sure what exactly it is that you're wanting. I'll provide two ways for you to decide.Arun
XElement xmlElement = new XElement(nameSpace + State.StateNewEmployeeRegistry);

            foreach (Employee employee in lstEmployee)

            {

                if(string.IsNullOrEmpty(employee.EmployeeMiddleName))

                {

                    continue; // skips adding this employee details

                }

                xmlElement.Add(new XElement(nameSpace + State.Employee, new XElement(nameSpace + Emp.EmployeeFirstName, employee.EmployeeFirstName),

                                new XElement(nameSpace + Emp.EmployeeMiddleName, employee.EmployeeMiddleName),

                                new XElement(nameSpace + Emp.EmployeeLastName, employee.EmployeeLastName),

                                new XElement(nameSpace + State.Address,

                                new XElement(nameSpace + State.UsAddress,

                                new XElement(nameSpace + Emp.EmployeeAddress1, employee.EmployeeAddress1),

                                new XElement(nameSpace + Emp.EmployeeAddress2, employee.EmployeeAddress2),

                                new XElement(nameSpace + Emp.EmployeeCity, employee.EmployeeCity),

                                new XElement(nameSpace + Emp.EmployeeState, employee.EmployeeState),

                                new XElement(nameSpace + Emp.EmployeeZipCode, employee.EmployeeZipCode))),

                                new XElement(nameSpace + Emp.EmployeeSsn, employee.Ssn)));

                xmlElement.Add(new XElement(nameSpace + Emp.EmployeeHireDate, employee.HireDate));



            }



// or 



XElement xmlElement = new XElement(nameSpace + State.StateNewEmployeeRegistry);

            foreach (Employee employee in lstEmployee)

            {

                xmlElement.Add(new XElement(nameSpace + State.Employee, new XElement(nameSpace + Emp.EmployeeFirstName, employee.EmployeeFirstName)));

                // add the middle name part only if it's not null

                if(!string.IsNullOrEmpty(employee.EmployeeMiddleName))

                {

                    xmlElement.Add(new XElement(nameSpace + Emp.EmployeeMiddleName, employee.EmployeeMiddleName)));

                }

                xmlElement.Add(new XElement(nameSpace + Emp.EmployeeLastName, employee.EmployeeLastName),

                                new XElement(nameSpace + State.Address,

                                new XElement(nameSpace + State.UsAddress,

                                new XElement(nameSpace + Emp.EmployeeAddress1, employee.EmployeeAddress1),

                                new XElement(nameSpace + Emp.EmployeeAddress2, employee.EmployeeAddress2),

                                new XElement(nameSpace + Emp.EmployeeCity, employee.EmployeeCity),

                                new XElement(nameSpace + Emp.EmployeeState, employee.EmployeeState),

                                new XElement(nameSpace + Emp.EmployeeZipCode, employee.EmployeeZipCode))),

                                new XElement(nameSpace + Emp.EmployeeSsn, employee.Ssn)));

                xmlElement.Add(new XElement(nameSpace + Emp.EmployeeHireDate, employee.HireDate));



            }

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33458426
Hi prashant-Vanam;

I have modified your code to do what you need.

Fernando
XElement xmlElement = new XElement(nameSpace + State.StateNewEmployeeRegistry);

foreach (Employee employee in lstEmployee)

{

    XElement newEmployee = (new XElement(nameSpace + State.Employee, new XElement(nameSpace + Emp.EmployeeFirstName, employee.EmployeeFirstName),

                    new XElement(nameSpace + Emp.EmployeeMiddleName, employee.EmployeeMiddleName),

                    new XElement(nameSpace + Emp.EmployeeLastName, employee.EmployeeLastName),

                    new XElement(nameSpace + State.Address,

                    new XElement(nameSpace + State.UsAddress,

                    new XElement(nameSpace + Emp.EmployeeAddress1, employee.EmployeeAddress1),

                    new XElement(nameSpace + Emp.EmployeeAddress2, employee.EmployeeAddress2),

                    new XElement(nameSpace + Emp.EmployeeCity, employee.EmployeeCity),

                    new XElement(nameSpace + Emp.EmployeeState, employee.EmployeeState),

                    new XElement(nameSpace + Emp.EmployeeZipCode, employee.EmployeeZipCode))),

                    new XElement(nameSpace + Emp.EmployeeSsn, employee.Ssn)));

    if (newEmployee.Element(nameSpace + "EmployeeMiddleName").Value == String.Empty) newEmployee.Element(nameSpace + "EmployeeMiddleName").Remove();                    

    newEmployee.Add(new XElement(nameSpace + Emp.EmployeeHireDate, employee.HireDate));

    xmlElement.Add(newEmployee);

}

Open in new window

0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33458504
Yeah ur the right......I want to skip the Middle initials if the value is Null....ur second solution is close but I want all the employee data in single Employee...what is happening right now is this
<Employee>
      <FirstName>ABDIMAJID</FirstName>
    </Employee>
    <MiddleInitial>B</MiddleInitial>
    <LastName>AASAJORDA</LastName>
    <Address>
      <USAddress>
        <AddressLine1>123 TALX St.</AddressLine1>
        <AddressLine2>123 TALX St.</AddressLine2>
        <City>VICTORIA</City>
        <State></State>
        <ZIPCode>3016</ZIPCode>
      </USAddress>
    </Address>
    <SSN>844633740</SSN>
    <StartOfWorkDate>2008-11-24</StartOfWorkDate>

what I want is

<Employee>
     <FirstName>ABDIMAJID</FirstName>
     <MiddleInitial>B</MiddleInitial>
    <LastName>AASAJORDA</LastName>
    <Address>
      <USAddress>
        <AddressLine1>123 TALX St.</AddressLine1>
        <AddressLine2>123 TALX St.</AddressLine2>
        <City>VICTORIA</City>
        <State></State>
        <ZIPCode>3016</ZIPCode>
      </USAddress>
    </Address>
    <SSN>844633740</SSN>
</Employee>
<StartOfWorkDate>2008-11-24</StartOfWorkDate>

Could you please see if i could get the output as I wanted
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:nmarun
ID: 33458594
Ok then try this.Arun
XElement xmlElement = new XElement(nameSpace + State.StateNewEmployeeRegistry);

            foreach (Employee employee in lstEmployee)

            {

                // add the middle name part only if it's not null

                if(!string.IsNullOrEmpty(employee.EmployeeMiddleName))

                {

                    xmlElement.Add(new XElement(nameSpace + State.Employee, new XElement(nameSpace + Emp.EmployeeFirstName, employee.EmployeeFirstName),

                                new XElement(nameSpace + Emp.EmployeeMiddleName, employee.EmployeeMiddleName),

                                new XElement(nameSpace + Emp.EmployeeLastName, employee.EmployeeLastName),

                                new XElement(nameSpace + State.Address,

                                new XElement(nameSpace + State.UsAddress,

                                new XElement(nameSpace + Emp.EmployeeAddress1, employee.EmployeeAddress1),

                                new XElement(nameSpace + Emp.EmployeeAddress2, employee.EmployeeAddress2),

                                new XElement(nameSpace + Emp.EmployeeCity, employee.EmployeeCity),

                                new XElement(nameSpace + Emp.EmployeeState, employee.EmployeeState),

                                new XElement(nameSpace + Emp.EmployeeZipCode, employee.EmployeeZipCode))),

                                new XElement(nameSpace + Emp.EmployeeSsn, employee.Ssn)));

                }

                else

                {

                     xmlElement.Add(new XElement(nameSpace + State.Employee, new XElement(nameSpace + Emp.EmployeeFirstName, employee.EmployeeFirstName),

                                new XElement(nameSpace + Emp.EmployeeLastName, employee.EmployeeLastName),

                                new XElement(nameSpace + State.Address,

                                new XElement(nameSpace + State.UsAddress,

                                new XElement(nameSpace + Emp.EmployeeAddress1, employee.EmployeeAddress1),

                                new XElement(nameSpace + Emp.EmployeeAddress2, employee.EmployeeAddress2),

                                new XElement(nameSpace + Emp.EmployeeCity, employee.EmployeeCity),

                                new XElement(nameSpace + Emp.EmployeeState, employee.EmployeeState),

                                new XElement(nameSpace + Emp.EmployeeZipCode, employee.EmployeeZipCode))),

                                new XElement(nameSpace + Emp.EmployeeSsn, employee.Ssn)));

                }

                xmlElement.Add(new XElement(nameSpace + Emp.EmployeeHireDate, employee.HireDate));

            }

Open in new window

0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33458660
Fernando..............my man. Ur code works exactly the way I wanted.
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33458681
Thanks for ur help nmarun...unfortunately I dont have anymore points to give you.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33459200
Not a problem, glad I was able to help.  ;=)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now