Search code examples
c#qgis

How to delete parent node <layer> in xml file using C#


I have such code

 <symbol type="marker" clip_to_extent="1" alpha="1" tags="multilayer,rgb,black,character,icomoon,yellow" name="60201010323" force_rhr="0">
      <layer locked="0" class="SvgMarker" enabled="1" pass="0">
        <prop v="0" k="angle"/>
        <prop v="255,255,0,255" k="color"/>
        <prop v="0" k="fixedAspectRatio"/>
        <prop v="1" k="horizontal_anchor_point"/>
        <prop v="60201010323.svg" k="name"/>
        <prop v="0,0" k="offset"/>
        <prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
        <prop v="Point" k="offset_unit"/>
        <prop v="35,35,35,255" k="outline_color"/>
        <prop v="0" k="outline_width"/>
        <prop v="3x:0,0,0,0,0,0" k="outline_width_map_unit_scale"/>
        <prop v="MM" k="outline_width_unit"/>
        <prop v="diameter" k="scale_method"/>
        <prop v="16.5326" k="size"/>
        <prop v="3x:0,0,0,0,0,0" k="size_map_unit_scale"/>
        <prop v="Point" k="size_unit"/>
        <prop v="1" k="vertical_anchor_point"/>
        <data_defined_properties>
          <Option type="Map">
            <Option type="QString" value="" name="name"/>
            <Option name="properties"/>
            <Option type="QString" value="collection" name="type"/>
          </Option>
        </data_defined_properties>
      </layer>
      <layer locked="1" class="SvgMarker" enabled="1" pass="0">
        <prop v="0" k="angle"/>
        <prop v="0,0,0,255" k="color"/>
        <prop v="0" k="fixedAspectRatio"/>
        <prop v="1" k="horizontal_anchor_point"/>
        <prop v="60201010323_1.svg" k="name"/>
        <prop v="0,0" k="offset"/>
        <prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
        <prop v="Point" k="offset_unit"/>
        <prop v="35,35,35,255" k="outline_color"/>
        <prop v="0" k="outline_width"/>
        <prop v="3x:0,0,0,0,0,0" k="outline_width_map_unit_scale"/>
        <prop v="MM" k="outline_width_unit"/>
        <prop v="diameter" k="scale_method"/>
        <prop v="14.8781" k="size"/>
        <prop v="3x:0,0,0,0,0,0" k="size_map_unit_scale"/>
        <prop v="Point" k="size_unit"/>
        <prop v="1" k="vertical_anchor_point"/>
        <data_defined_properties>
          <Option type="Map">
            <Option type="QString" value="" name="name"/>
            <Option name="properties"/>
            <Option type="QString" value="collection" name="type"/>
          </Option>
        </data_defined_properties>
      </layer>
      <layer locked="1" class="SvgMarker" enabled="1" pass="0">
        <prop v="0" k="angle"/>
        <prop v="0,0,0,255" k="color"/>
        <prop v="0" k="fixedAspectRatio"/>
        <prop v="1" k="horizontal_anchor_point"/>
        <prop v="60201010323_2.svg" k="name"/>
        <prop v="0,0" k="offset"/>
        <prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
        <prop v="Point" k="offset_unit"/>
        <prop v="35,35,35,255" k="outline_color"/>
        <prop v="0" k="outline_width"/>
        <prop v="3x:0,0,0,0,0,0" k="outline_width_map_unit_scale"/>
        <prop v="MM" k="outline_width_unit"/>
        <prop v="diameter" k="scale_method"/>
        <prop v="16.5326" k="size"/>
        <prop v="3x:0,0,0,0,0,0" k="size_map_unit_scale"/>
        <prop v="Point" k="size_unit"/>
        <prop v="1" k="vertical_anchor_point"/>
        <data_defined_properties>
          <Option type="Map">
            <Option type="QString" value="" name="name"/>
            <Option name="properties"/>
            <Option type="QString" value="collection" name="type"/>
          </Option>
        </data_defined_properties>
      </layer>
    </symbol>

I want to delete some layer in symbol type="marker" where <layer locked="1" and prop v="60201010323_1.svg" k="name"/(contains underscore _)

So it's should be looked like this

 <symbol type="marker" clip_to_extent="1" alpha="1" tags="multilayer,rgb,black,character,icomoon,yellow" name="60201010323" force_rhr="0">
      <layer locked="0" class="SvgMarker" enabled="1" pass="0">
        <prop v="0" k="angle"/>
        <prop v="255,255,0,255" k="color"/>
        <prop v="0" k="fixedAspectRatio"/>
        <prop v="1" k="horizontal_anchor_point"/>
        <prop v="60201010323.svg" k="name"/>
        <prop v="0,0" k="offset"/>
        <prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
        <prop v="Point" k="offset_unit"/>
        <prop v="35,35,35,255" k="outline_color"/>
        <prop v="0" k="outline_width"/>
        <prop v="3x:0,0,0,0,0,0" k="outline_width_map_unit_scale"/>
        <prop v="MM" k="outline_width_unit"/>
        <prop v="diameter" k="scale_method"/>
        <prop v="16.5326" k="size"/>
        <prop v="3x:0,0,0,0,0,0" k="size_map_unit_scale"/>
        <prop v="Point" k="size_unit"/>
        <prop v="1" k="vertical_anchor_point"/>
        <data_defined_properties>
          <Option type="Map">
            <Option type="QString" value="" name="name"/>
            <Option name="properties"/>
            <Option type="QString" value="collection" name="type"/>
          </Option>
        </data_defined_properties>
      </layer>
</symbol>

p.s. i know that it's a bad behaviour here to ask for help, but i am not a programmer, i just have 300K lines of code and most of them are not needed, please help by providing the c# code


Solution

  • Very simple using Xml Linq :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
    
                List<XElement> toDelete = doc.Descendants("layer")
                    .Where(XElement => ((int)XElement.Attribute("locked") == 0) && ((string)XElement.Attribute("class") == "SvgMarker"))
                    .ToList();
    
                toDelete.Remove();
    
                doc.Save(FILENAME);
    
            }
         }
    }