c#从xml文件中删除节点,而不考虑层次结构

时间:2018-01-31 12:06:15

标签: c# xml linq

请参阅我的演示。我想删除所有重复的地标节点,而不管层次结构如何。这对Linq有可能吗?删除所有重复节点并保存为新的xml文件的最佳方法是什么。

另请注意,对于演示,我创建了一个字符串,否则我将从计算机路径加载xml文件。

https://dotnetfiddle.net/vAA6Xu

更新

    var raw = @"<?xml version='1.0' encoding='UTF-8'?><kml xmlns= 'http://www.opengis.net/kml/2.2'>
      <Document>
          <Folder>
  <Placemark>
    <name>1</name>
    <description>abc</description>
    <Point>
      <coordinates>102.595626,14.996729</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>2</name>
    <description>abc</description>
    <Point>
      <coordinates>102.595626,14.996729</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>2</name>
    <description>abc</description>
    <Point>
      <coordinates>102.595626,14.996729</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>4</name>
    <description>abc</description>
    <Point>
      <coordinates>102.595626,14.996729</coordinates>
    </Point>
  </Placemark>

   <Folder>
          <Placemark>
            <name>3</name>
            <description>abc</description>
            <Point>
              <coordinates>102.595626,14.996729</coordinates>
            </Point>
          </Placemark>
          <Placemark>
            <name>4</name>
            <description>abc</description>
            <Point>
              <coordinates>102.595626,14.996729</coordinates>
            </Point>
          </Placemark>
          <Placemark>
            <name>4</name>
            <description>abc</description>
            <Point>
              <coordinates>102.595626,14.996729</coordinates>
            </Point>
          </Placemark>
</Folder>
</Folder> 
<Placemark>
  <name>4</name>
  <description>abc</description>
  <Point>
    <coordinates>102.595626,14.996729</coordinates>
  </Point>
</Placemark>

<Placemark>
  <name>4</name>
  <description>abc</description>
  <Point>
    <coordinates>102.595626,14.996729</coordinates>
  </Point>
</Placemark>
      </Document>
    </kml>";

代码

    var xdoc = XDocument.Parse(raw);
    XNamespace ns = "http://www.opengis.net/kml/2.2";
    xdoc.Root.Elements(ns+"Document").Elements(ns+"Placemark").GroupBy(i => (string)i).SelectMany(g => g.Skip(1)).Remove();

我想查找所有Placemark节点,并删除重复的节点,而不考虑层次结构

1 个答案:

答案 0 :(得分:0)

您可以通过将地标节点的值连接到字典来实现 如果您在字典中找到节点的值,则它是重复的,您可以在其上调用.Remove() 一种方法:

var raw = "yourxmlhere";
var xdoc = XDocument.Parse(raw);
XNamespace ns = "http://www.opengis.net/kml/2.2";

Dictionary<string,bool> dictionary = new Dictionary<string,bool>();
var descendants = xdoc.Root.Descendants(ns + "Placemark").ToList();

foreach(var node in descendants){
    string key = string.Join("", node.Elements().Select(el => el.Value));
    if(dictionary.ContainsKey(key))
        node.Remove();
    else
        dictionary.Add(key, true);
}
//at this point your xdoc only contains unique Placemark nodes