Solved

skip XElement if the value is empty

Posted on 2010-08-17
8
1,028 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Suggested Solutions

Title # Comments Views Activity
Vb.net threads keep increasing 2 59
How to repeat the data 4 43
How can I do Unit Testing with Session ? 1 41
Office 365: Assigning MailboxPlan to a mailbox 8 69
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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