Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Removal of XML element after LINQ query

Posted on 2009-05-14
3
Medium Priority
?
292 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
[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
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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