C#Count删除了节点

时间:2018-01-30 11:01:46

标签: c# xml linq

我有一个xml文件,如果它们是重复的,我想删除一些节点。我想删除可能在Document节点或Folder节点中的地标。我还想找到有多少被删除所以我写了这段代码

                var xDoc = XDocument.Load(kmlFileUrl);
                XNamespace ns = "http://www.opengis.net/kml/2.2";

                var countInFolder = xDoc.Root
                .Elements(ns + "Document")
                .Elements(ns + "Folder")
                .Elements(ns + "Placemark")
                .GroupBy(i => (string)i)
                .SelectMany(g => g.Skip(1)).Count();

                xDoc.Root
                .Elements(ns + "Document")
                .Elements(ns + "Folder")
                .Elements(ns + "Placemark")
                .GroupBy(i => (string)i)
                .SelectMany(g => g.Skip(1))
                .Remove();

                var countInDocument = xDoc.Root
               .Elements(ns + "Document")                   
               .Elements(ns + "Placemark")
               .GroupBy(i => (string)i)
               .SelectMany(g => g.Skip(1)).Count();

                xDoc.Root
               .Elements(ns + "Document")                   
               .Elements(ns + "Placemark")
               .GroupBy(i => (string)i)
               .SelectMany(g => g.Skip(1))
               .Remove();

                int totalRemoved = int.Parse(countInFolder.ToString()) + int.Parse(countInDocument.ToString());

请您检查并建议是否可以进一步改进此代码,或者我可以将代码中的某些行组合在一起,或者这是删除和计算它们的正确方法。

编辑:xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">

  <Document>
    <name>KML Samples</name>
    <open>1</open>
    <description>Unleash your creativity with the help of these examples!</description>

    <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>

    <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>

    <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>

编辑2:我想简单地说。如何确保从xml文件中删除所有重复的Placemark模式,而不管它们进入哪个节点,即。它可以在文件夹节点或文档节点内或任何地方,所以我想删除所有重复的。

1 个答案:

答案 0 :(得分:1)

这就是我改进代码的方式:

var xDoc = XDocument.Load(kmlFileUrl);
XNamespace ns = "http://www.opengis.net/kml/2.2";

var elementsInFolder = xDoc.Root
    .Elements(ns + "Document")
    .Elements(ns + "Folder")
    .Elements(ns + "Placemark")
    .GroupBy(i => (string) i)
    .SelectMany(g => g.Skip(1))
    .ToList();

var countInFolder = elementsInFolder.Count;
elementsInFolder.Remove();

var elementsInDocument = xDoc.Root
    .Elements(ns + "Document")
    .Elements(ns + "Placemark")
    .GroupBy(i => (string) i)
    .SelectMany(g => g.Skip(1))
    .ToList();

var countInDocument = elementsInDocument.Count;
elementsInDocument.Remove();

int totalRemoved = countInFolder + countInDocument;

在计算和删除临时结果之前,只需将临时结果保存到List。此外,将整数转换为字符串并解析它是没有意义的,只需根据需要使用整数。