Solved

How to delete all empty XElements from a XDocument

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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