Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Removal of XML element after LINQ query

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
brenlex
Asked:
brenlex
  • 2
1 Solution
 
Fernando SotoCommented:
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
 
brenlexAuthor Commented:
Perfect. Many thanks.
0
 
Fernando SotoCommented:
Not a problem, glad to help.  ;=)
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now