Solved

How to delete all empty XElements from a XDocument

Posted on 2010-08-17
5
1,308 Views
Last Modified: 2013-12-17
How to delete all empty XElements from a XDocument
0
Comment
Question by:prashant-Vanam
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33456049
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XElement firstParticipant;
        XDocument xDocument = new XDocument(
          new XElement("Books", firstParticipant =
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "J"),
              new XElement("LastName", "R"))));
       
        firstParticipant.Attribute("type").Remove();
        Console.WriteLine(xDocument);
    }
}

something like this. you need to loop through the all attribute of the xdocument to delete the empty elements.
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33456589
Here is my sample XML now can you tell me how to look for empty Employee MiddleInitial and delete if we find any.
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<ReturnDataState xsi:schemaLocation="http://www.irs.gov/efile ReturnDataState.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.irs.gov/efile">
  <ContentLocation>Company </ContentLocation>
  <ReturnHeaderState>
    <PreparerFirm>
      <BusinessName>
        <BusinessNameLine1>Company CORPORATION</BusinessNameLine1>
      </BusinessName>
      <Address>
        <USAddress>
          <AddressLine1>11 Rd</AddressLine1>
          <AddressLine2>Some Dummy</AddressLine2>
          <City>ST Louis</City>
          <State>MO</State>
          <ZIPCode>63146</ZIPCode>
        </USAddress>
      </Address>
    </PreparerFirm>
    <ReturnType>StateNewEmployeeRegistry</ReturnType>
    <StateEIN>
      <TypeStateEIN>WithholdingAccountNo</TypeStateEIN>
      <StateEINValue>05146279</StateEINValue>
    </StateEIN>
    <StateCode>CA</StateCode>
  </ReturnHeaderState>
  <StateGeneralInformation>
    <BusinessAddress>
      <BusinessName>
        <BusinessNameLine1>MIILIG Inc</BusinessNameLine1>
      </BusinessName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>WOODBRIDGE</City>
          <State>NJ</State>
          <ZIPCode>070950961</ZIPCode>
        </USAddress>
      </Address>
    </BusinessAddress>
  </StateGeneralInformation>
  <StateNewEmployeeRegistry>
    <Employee>
      <FirstName>ABDIMAJID</FirstName>
      <MiddleInitial>B</MiddleInitial>
      <LastName>AASAJORDA</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>VICTORIA</City>
          <State></State>
          <ZIPCode>3016</ZIPCode>
        </USAddress>
      </Address>
      <SSN>844633740</SSN>
    </Employee>
    <StartOfWorkDate>2008-11-24</StartOfWorkDate>
    <Employee>
      <FirstName>ABDOLYNNEM</FirstName>
      <MiddleInitial></MiddleInitial>
      <LastName>ABDRABBOH</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>KUALA LUMPUR</City>
          <State></State>
          <ZIPCode>50400</ZIPCode>
        </USAddress>
      </Address>
      <SSN>845480805</SSN>
    </Employee>
    <StartOfWorkDate>2008-02-14</StartOfWorkDate>
    <Employee>
      <FirstName>ABUTAIB</FirstName>
      <MiddleInitial>N</MiddleInitial>
      <LastName>BASHARII</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>QUITO</City>
          <State></State>
          <ZIPCode></ZIPCode>
        </USAddress>
      </Address>
      <SSN>842210858</SSN>
    </Employee>
    <StartOfWorkDate>2006-04-06</StartOfWorkDate>
    <Employee>
      <FirstName>AALY</FirstName>
      <MiddleInitial>R</MiddleInitial>
      <LastName>ABARNES</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>INVERMERE</City>
          <State></State>
          <ZIPCode>113</ZIPCode>
        </USAddress>
      </Address>
      <SSN>863562736</SSN>
    </Employee>
    <StartOfWorkDate>2006-02-09</StartOfWorkDate>
    <Employee>
      <FirstName>ABDINAFIR</FirstName>
      <MiddleInitial></MiddleInitial>
      <LastName>BARNFA</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>VAUXHALL</City>
          <State></State>
          <ZIPCode>020</ZIPCode>
        </USAddress>
      </Address>
      <SSN>863509740</SSN>
    </Employee>
    <StartOfWorkDate>2006-08-20</StartOfWorkDate>
    <Employee>
      <FirstName>ABDIK</FirstName>
      <MiddleInitial></MiddleInitial>
      <LastName>ACEDENO</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>JAKARTA SELETAN</City>
          <State></State>
          <ZIPCode>12430</ZIPCode>
        </USAddress>
      </Address>
      <SSN>819701897</SSN>
    </Employee>
    <StartOfWorkDate>2005-10-25</StartOfWorkDate>
    <Employee>
      <FirstName>ABDERRAF</FirstName>
      <MiddleInitial></MiddleInitial>
      <LastName>AMIZOL</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>BOGOTA</City>
          <State></State>
          <ZIPCode></ZIPCode>
        </USAddress>
      </Address>
      <SSN>803157415</SSN>
    </Employee>
    <StartOfWorkDate>2003-05-20</StartOfWorkDate>
    <Employee>
      <FirstName>ABDOW</FirstName>
      <MiddleInitial>P</MiddleInitial>
      <LastName>ANDROVICH</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>WAINSCOTT</City>
          <State></State>
          <ZIPCode>11976</ZIPCode>
        </USAddress>
      </Address>
      <SSN>863578882</SSN>
    </Employee>
    <StartOfWorkDate>2004-05-25</StartOfWorkDate>
    <Employee>
      <FirstName>ALJIN</FirstName>
      <MiddleInitial></MiddleInitial>
      <LastName>ANDROUTSOPOULOS</LastName>
      <Address>
        <USAddress>
          <AddressLine1>123 Company St.</AddressLine1>
          <AddressLine2>123 Company St.</AddressLine2>
          <City>WAINSCOTT</City>
          <State></State>
          <ZIPCode>11976</ZIPCode>
        </USAddress>
      </Address>
      <SSN>863578879</SSN>
    </Employee>
    <StartOfWorkDate>2004-05-25</StartOfWorkDate>
  </StateNewEmployeeRegistry>
</ReturnDataState>

Open in new window

0
 
LVL 63

Accepted Solution

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

The below code will do what you need.

Fernando
XDocument xdoc = XDocument.Load("Sample.xml");
XNamespace ns = xdoc.Root.Name.Namespace;

var employees = from emn in xdoc.Root.Descendants(ns + "StateNewEmployeeRegistry").Elements(ns + "Employee")
                where emn.Element(ns + "MiddleInitial").Value == String.Empty
                select emn;

foreach (XElement emp in employees)
{
    emp.Element(ns + "MiddleInitial").Remove();
}

xdoc.Save("Results.xml");

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33457688
Hi prashant-Vanam;

The important thing here is to make sure you use the XNamespace otherwise you will never get any results.

Fernando
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33458249
Fernando.....could you please answer this question too. http://www.experts-exchange.com/Programming/Languages/.NET/LINQ/Q_26409494.html 

Thanks for ur help
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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 …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

807 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