【问题标题】:Deleting set of XML record based on duplicate Element value删除基于重复元素值的 XML 记录集
【发布时间】:2020-11-05 05:19:09
【问题描述】:

我有以下 XML 文件:

<RHL_IncentiveGroup>
  <ID>5eaaf7fd-68f5-430b-ade5-00ad1b952fc2</ID>
  <Name>Legacy_QReward_Sojos Cat 1lb</Name>
  <Description>Migrated from Q-Reward</Description>
  <RequirePermission>false</RequirePermission>
  <SyncGuid>0003d9cb-e39a-4fd1-bda6-d2608eb29d05</SyncGuid>
</RHL_IncentiveGroup>
<RHL_IncentiveGroup>
  <ID>0ab4f2d5-ad86-4e56-a6ce-00e1dacd041f</ID>
  <Name>Legacy_QReward_Sojos Cat 1lb</Name>
  <Description>Migrated from Q-Reward</Description>
  <RequirePermission>false</RequirePermission>
  <SyncGuid>000abbcf-2ef2-41ed-84fb-80de503e42b3</SyncGuid>
</RHL_IncentiveGroup>
<RHL_IncentiveGroup>
  <ID>bda6cc8b-3608-49e6-8720-024e6ee75434</ID>
  <Name>Legacy_QReward_TOW Dog 28lbs</Name>
  <Description>Migrated from Q-Reward</Description>
  <RequirePermission>false</RequirePermission>
  <SyncGuid>00096931-bc97-4f6d-8510-e6ccf63f6dc8</SyncGuid>
</RHL_IncentiveGroup>

我想删除那些Name 相同的RHL_IncentiveGroup 节点,只保留一个。

例如其中NameLegacy_QReward_Sojos Cat 1lb

【问题讨论】:

  • 你研究了什么,什么不适合你?您还使用了 SQL 标记,这与 SQL 有什么关系?
  • @AmritpalSingh 你知道不是格式良好的 XML 吗?缺少根元素。

标签: c# sql xml linq duplicates


【解决方案1】:

假设您有以下对象模型,您可以在其中反序列化 xml:

public class RHLIncentiveGroup
{
    [XmlElement(elementName:"ID")]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool RequirePermission { get; set; }
    public Guid SyncGuid { get; set; }
}

为了使您的 XML 格式正确,我添加了一个 &lt;groups&gt; 节点来包围您的 &lt;RHL_IncentiveGroup&gt; 元素。因此,我们还必须为顶级节点创建类:

[XmlRoot("groups", Namespace = "")]
public class Groups
{
    [XmlElement("RHL_IncentiveGroup")]
    public List<RHLIncentiveGroup> Items { get; set; }
}

让我们将 xml 反序列化为 Groups 类实例:

XDocument doc = XDocument.Parse(originalXml);
var serializer = new XmlSerializer(typeof(Groups));
var withDuplicates = (Groups) serializer.Deserialize(doc.CreateReader());

现在,让我们删除重复项:

var withoutDuplicates = withDuplicates.Items
    .GroupBy(groupNode => groupNode.Name)
    .Select(nameGroupNodes => nameGroupNodes.First());

最后将其序列化回xml:

var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);

var writer = new StringWriter();
serializer.Serialize(writer, withoutDuplicates, ns);

var updatedXml = writer.ToString();

【讨论】:

    【解决方案2】:

    这变得很复杂,因为您在根级别有多个标签。这是使用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)
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ConformanceLevel = ConformanceLevel.Fragment;
                XmlReader reader = XmlReader.Create(FILENAME, settings);
    
                List<XElement> RHLgroups = new List<XElement>();
    
                while (!reader.EOF)
                {
                    if (reader.Name != "RHL_IncentiveGroup")
                    {
                        reader.ReadToFollowing("RHL_IncentiveGroup");
                    }
                    if (!reader.EOF)
                    {
                        RHLgroups.Add((XElement)XElement.ReadFrom(reader));
                    }
                }
    
                var groups = RHLgroups
                    .GroupBy(x => (string)x.Element("Name"))
                    .Select(x => x.FirstOrDefault())
                    .ToList();
            
            }
          
     
        }
    
    }
    

    留下一个最简单的方法是复制到新列表中并将原始列表设置为空。

               var groups = RHLgroups
                    .GroupBy(x => (string)x.Element("Name"))
                    .Select(x => XElement.Parse(x.FirstOrDefault().ToString()))
                    .ToList();
    
                RHLgroups = null;
    

    【讨论】:

    • 如何删除包括子节点在内的整条记录?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2021-03-28
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2018-05-01
    • 2016-04-26
    相关资源
    最近更新 更多