Solved

skip XElement if the value is empty

Posted on 2010-08-17
8
1,027 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 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

762 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