Solved

How to delete all empty XElements from a XDocument

Posted on 2010-08-17
5
1,279 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 62

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 62

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

15 Experts available now in Live!

Get 1:1 Help Now