Solved

How to delete all empty XElements from a XDocument

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

9 Experts available now in Live!

Get 1:1 Help Now