Solved

How to delete all empty XElements from a XDocument

Posted on 2010-08-17
5
1,320 Views
Last Modified: 2013-12-17
How to delete all empty XElements from a XDocument
0
Comment
Question by:prashant-Vanam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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