Solved

skip XElement if the value is empty

Posted on 2010-08-17
8
1,031 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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
 
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 63

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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