Solved

Removal of XML element after LINQ query

Posted on 2009-05-14
3
285 Views
Last Modified: 2013-12-17
My XML doc content:

<?xml version="1.0" encoding="utf-8"?>
<Main>
  <Members type="">
    <Member>
      <Primary notes="">100</Primary>
      <Secondary notes="Phone">5100</Secondary>
    </Member>
    <Member>
      <Primary notes="">200</Primary>
      <Secondary notes="Phone">5200</Secondary>
    </Member>
    <Member>
      <Primary notes="">200</Primary>
      <Secondary notes="Fax">6200</Secondary>
    </Member>
  </Members>
</Main

I am experiencing a problem whereby I cannot remove a Member element in its entirety -- i.e. after running the code below, I always have a blank entry left in the file:    

<Member />

The blank entries are causing me problems later when I attempt additional queries on the data.
m_xmlDoc = XDocument.Load("test.xml");
 

IEnumerable<XElement> membersMap = from memberRef in m_xmlDoc.Descendants("Member")

                                    where memberRef.Element("Primary").Value == "200"

                                    select memberRef;

                    foreach (XElement memberRef in membersMap)

                    {

                        memberRef.RemoveAll(); // remove Primary and Secondary elements

                        //memberRef.Remove(); // this causes an exception!

                    }

m_xmlDoc.Save("test.xml");

Open in new window

0
Comment
Question by:brenlex
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 24386134
Hi brenlex;

From Microsoft documentation: "In LINQ to XML programming, you should not manipulate or modify a set of nodes while you are querying for nodes in that set. In practical terms, this means that you should not iterate over a set of nodes and remove them. Instead, you should materialize them into a List<(Of <(T>)>) by using the ToList<(Of <(TSource>)>) extension method. Then, you can iterate over the list to remove the nodes.",
http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.remove.aspx

The folloing code snippet will work.

Fernando


m_xmlDoc = XDocument.Load("test.xml");
 

IEnumerable<XElement> membersMap = from memberRef in m_xmlDoc.Descendants("Member")

                                   where memberRef.Element("Primary").Value == "200"

                                   select memberRef;

                                   

foreach (XElement memberRef in membersMap.ToList())

{

    memberRef.Remove(); 

}

m_xmlDoc.Save("test.xml");

Open in new window

0
 

Author Closing Comment

by:brenlex
ID: 31581431
Perfect. Many thanks.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24395026
Not a problem, glad to help.  ;=)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
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 …

929 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

7 Experts available now in Live!

Get 1:1 Help Now