删除Xml中的重复文档

问题描述:

我有两个包含产品列表的XML文档。目前,我们只复制一个并将其粘贴到另一个中,然后创建一个新的合并文档,但是,这两个文件具有许多相同的产品,因此我需要合并两个并删除重复项。我的XML文档的结构如下:删除Xml中的重复文档

<?xml version="1.0" encoding="iso-8859-1"?> 
    <table> 
     <row Code="HST15154" 
     ProductName="test" 
     ProductName_EN="" 
     Description_EN="" 
     Price="" 
     ProductType1="HST ACCESSORIES" 
     ProductType2="SAM - Accessories" 
     ProductCategory="Accessories" 
     Remarks="" 
     /> 
    </table> 

我发现一些代码,我试图改变我的需求here。我只需要每个“代码”中的一个。

using System; 
using System.Collections.Generic; 
using System.Xml; 

namespace HST_Merging_Console_App 
{ 
    public class Program 
    { 
     public void Main(string[] args) 
     { 
      //open the xml document 
      XmlDocument doc = new XmlDocument(); 
      doc.LoadXml("U:\\Documents (U)\\XML Merging Tool\\productcollection_us.xml"); 

      //select all row elements 
      XmlNodeList parts = doc.SelectNodes("/row"); 

      //create a list of previously seen P/Ns 
      List<string> PartsSeen = new List<string>(); 

      foreach(XmlNode part in parts) 
      { 
       string partNumber = part.Attributes["Code"].Value; 

       //for each part, see if we have seen it before, if it is in the list, 
       //remove the part element from the parent to which it belongs 
       if (PartsSeen.Contains(partNumber)) 
        part.ParentNode.RemoveChild(part); 
       else 
        PartsSeen.Add(partNumber); 
      } 
      Console.Read(); 
      doc.Save("U:\\Documents (U)\\XML Merging Tool\\productcollection_merged.xml"); 
     } 
    } 
} 

我收到了几个错误,当我运行此:

CS1061 - “XmlDocument的”不包含“的SelectNodes”的定义,并没有扩展方法“的SelectNodes”接受第一个参数型“的XmlDocument”的可以找到(是否缺少using指令或程序集引用?)(第16行)

CS1503 - 参数1:不能从“字符串”转换为“System.IO.Stream” (第33行)

我考虑过的另一种方法是将第一个文件加载到数据集中,然后取第二个文件并将其加载到第二个数据集中。然后遍历第二个数据集搜索第一个数据集中的代码,如果发现更新该行,如果没有,则添加该行。

这是我第一次使用C#并尝试创建一个程序在服务器上运行。任何帮助和/或建议非常感谢。

+0

这是什么类型的应用程序?你可以用XDocument来代替吗? – sr28

+0

我想做一个.net应用程序。另外,我正在使用Visual Studios 2015. – cheshire

+0

你认为重复的是什么?一切都一样吗?或者它们都有唯一的代码,例如只需要匹配就可以重复? – sr28

使用LINQ to Xml
HashSet你可以识别重复的代码。如果集合中已经存在相同的值,则HashSet.Add()将返回false。

var doc = XDocument.Load(yourPath); 
var codes = new HashSet<string>(); 

// .ToList() is important for removing elements 
foreach(var row in doc.Root.Elements("row").ToList()) 
{ 
    var code = row.Attribute("Code").Value; 
    var isUniqueCode = codes.Add(code); 
    if(isUniqueCode == false) 
    { 
     row.Remove(); 
    } 
} 

doc.Save(newPath); 
+0

当我尝试这样做时,我得到“'XDocument'在当前上下文中不存在”,如果我尝试“XmlDocument”,则会出现几个错误:“无法将void分配给隐式类型变量“和”非静态字段,方法或属性需要对象引用'XmlDocument.Load(string)'“ – cheshire

+0

使用System.Xml.Linq添加' – Fabio

+0

是否有另一个引用我需要为ToList ()?这显示为现在没有找到。 – cheshire

你可以在一个更简单的方法做到这一点,尝试这样的事情:

var uniques = doc.Descendants("row").Attributes("Code").Distinct() 

我没有测试过这虽然所以它可能需要一些修改

您可以使用XDocument代替,这是一个比较容易使用XmlDocument的。使用时,您需要以using System.Xml.Linq。然后简单地按照“Code”属性对LINQ to XML进行分组:

XDocument doc = XDocument.Load("U:\\Documents (U)\\XML Merging Tool\\productcollection_us.xml"); 

var uniqueProducts = doc.Root.Elements("row").GroupBy(x => (string)x.Attribute("Code"));