1

How would i using Linq remove all section where their element contains parameter with {} ? In my example i want to remove section with {SecName1}

Source document:

<ReceiptLayoutMaintenanceRequest>
  <ReceiptLayoutName>Test Layout1</ReceiptLayoutName> 
  <ActionName>Add</ActionName> 
 <ReceiptLayoutForMaintenance>
  <Name>Test Layout1</Name> 
  <Description>ReciptDesc</Description> 
  <PrinterName>Emulator - Receipt</PrinterName> 
  <ReceiptLayout>
    <Name>AAA</Name> 
    <Description>$</Description> 
    <TemplateName>DefaultTemplate</TemplateName> 
    <LayoutParameters /> 
  </ReceiptLayout>
  <ReceiptLayout>
    <Name>{SecName1}</Name> 
    <Description>$</Description> 
    <TemplateName>DefaultTemplate</TemplateName> 
    <LayoutParameters /> 
  </ReceiptLayout>
 </ReceiptLayoutForMaintenance>
</ReceiptLayoutMaintenanceRequest>

Wanted output

<ReceiptLayoutMaintenanceRequest>
  <ReceiptLayoutName>Test Layout1</ReceiptLayoutName> 
  <ActionName>Add</ActionName> 
 <ReceiptLayoutForMaintenance>
  <Name>AAA</Name> 
  <Description>ReciptDesc</Description> 
  <PrinterName>Emulator - Receipt</PrinterName> 
  <ReceiptLayout>
    <Name>AAA</Name> 
    <Description>$</Description> 
    <TemplateName>DefaultTemplate</TemplateName> 
    <LayoutParameters /> 
  </ReceiptLayout>
 </ReceiptLayoutForMaintenance>

thanks

user681055
  • 59
  • 11

3 Answers3

2

This removes any ReceiptLayout node which has a child Name that starts and ends with brackets and produces your desired output:

XDocument doc = XDocument.Load(@"test.xml"); //load xml
var nodesToRemove = doc.Descendants("ReceiptLayout")
                       .Where(x => x.Element("Name").Value.StartsWith("{") 
                                && x.Element("Name").Value.EndsWith("}"))
                       .ToList();

foreach (var node in nodesToRemove)
    node.Remove();

This can be shortened into one Linq statement, personally I prefer to keep Linq query and modification (removal) separate though:

doc.Descendants("ReceiptLayout")
   .Where(x => x.Element("Name").Value.StartsWith("{") 
            && x.Element("Name").Value.EndsWith("}"))
   .Remove();
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1
var doc =  XDocument.Parse(xml);
doc.Descendants()
   .Where(n => !n.HasElements  && Regex.IsMatch(n.Value, "[{].*?[}]"))
   .Select(n=>n.Parent) // because you want to remove the section not the node
   .Remove();
xml = doc.ToString();
Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
1

A variant of BrokenGlass code using the let keyword

var doc = XDocument.Load(@"test.xml");

var list = from p in doc.Descendants("ReceiptLayout")
           let q = p.Element("Name")
           let r = q != null ? q.Value : string.Empty
           where r.StartsWith("{") && r.EndsWith("}")
           select p;

list.Remove();

This is "premature optimization" :-) I "cache" the p.Element("Name").Value. Ah... And I check if really there is a Name Element so that everything doesn't crash if there isn't one :-)

xanatos
  • 109,618
  • 12
  • 197
  • 280