Solved

skip XElement if the value is empty

Posted on 2010-08-17
8
1,024 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

25 Experts available now in Live!

Get 1:1 Help Now