C# 删除Xml文件中的重复节点

C# 删除Xml文件中的重复节点,c#,xml,linq,C#,Xml,Linq,我需要从xml文档中删除重复的字段。我有一个linq查询,它根据是否有多个匹配的id属性收集所有重复项 代码: 现在我在删除一个ID时遇到了问题。目前,我删除它的方式会删除所有引用,因此它不会删除重复项,而是同时删除重复项和第一个引用 您知道如何使用linq查询实现这一点吗?您可以使用跳过(1)从每个组中获取除第一个之外的所有元素,然后调用这些选定元素: xdoc.Descendants("Field") .GroupBy(g => (string)g.Attribute("id"

我需要从xml文档中删除重复的字段。我有一个linq查询,它根据是否有多个匹配的id属性收集所有重复项

代码:

现在我在删除一个ID时遇到了问题。目前,我删除它的方式会删除所有引用,因此它不会删除重复项,而是同时删除重复项和第一个引用

您知道如何使用linq查询实现这一点吗?

您可以使用
跳过(1)
从每个组中获取除第一个之外的所有元素,然后调用这些选定元素:

xdoc.Descendants("Field")
    .GroupBy(g => (string)g.Attribute("id"))
    .SelectMany(g => g.Skip(1))
    .Remove();
您需要属性(“id”)。值才能使代码正常工作。试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<Root>" +
              "<Field id =\"1\"></Field>" +
              "<Field id =\"2\"></Field>" +
              "<Field id =\"3\"></Field>" +
              "<Field id =\"1\"></Field>" +
              "<Field id =\"2\"></Field>" +
              "<Field id =\"3\"></Field>" +
              "<Field id =\"4\"></Field>" +
              "<Field id =\"2\"></Field>" +
              "<Field id =\"3\"></Field>" +
              "<Field id =\"4\"></Field>" +
            "</Root>";

            XDocument xdoc = XDocument.Parse(input);

            var xtra =
                xdoc.Descendants("Field")
                .GroupBy(g => g.Attribute("id").Value)
                .Select(x => x.FirstOrDefault())
                .ToList();

        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XDocument xdoc=XDocument.Parse(输入);
var xtra=
xdoc.子体(“字段”)
.GroupBy(g=>g.Attribute(“id”).Value)
.Select(x=>x.FirstOrDefault())
.ToList();
}
}
}
​
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<Root>" +
              "<Field id =\"1\"></Field>" +
              "<Field id =\"2\"></Field>" +
              "<Field id =\"3\"></Field>" +
              "<Field id =\"1\"></Field>" +
              "<Field id =\"2\"></Field>" +
              "<Field id =\"3\"></Field>" +
              "<Field id =\"4\"></Field>" +
              "<Field id =\"2\"></Field>" +
              "<Field id =\"3\"></Field>" +
              "<Field id =\"4\"></Field>" +
            "</Root>";

            XDocument xdoc = XDocument.Parse(input);

            var xtra =
                xdoc.Descendants("Field")
                .GroupBy(g => g.Attribute("id").Value)
                .Select(x => x.FirstOrDefault())
                .ToList();

        }
    }
}
​