如何使用 C# 删除 xml 文件中的父节点 <layer>
How to delete parent node <layer> in xml file using C#
我有这样的代码
<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>
我想删除一些图层
在符号类型="marker"中
<layer locked="1" and prop v="60201010323_1.svg" k="name"/(包含下划线 _)
所以它应该看起来像这样
<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。我知道在这里寻求帮助是一种不好的行为,但我不是程序员,我只有 300K 行代码,其中大部分都不需要,请提供 c# 代码
使用 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);
}
}
}
我有这样的代码
<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>
我想删除一些图层 在符号类型="marker"中 <layer locked="1" and prop v="60201010323_1.svg" k="name"/(包含下划线 _)
所以它应该看起来像这样
<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。我知道在这里寻求帮助是一种不好的行为,但我不是程序员,我只有 300K 行代码,其中大部分都不需要,请提供 c# 代码
使用 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);
}
}
}