问题
I use Xml.Linq for manage xml configuration files.
I have XElement (Company.CalidadCodigo.ParserSQL.Reglas), and I need remove duplicate values in XElement (nodes Add-Key-Value, which Value is repeated).
I use Union but not right.
var reglasComunes = reglasParaTarget.Union(reglasParaSecundario);
Any sample code about it?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Company.CalidadCodigo.ParserSQL.Reglas" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<appSettings></appSettings>
<Company.CalidadCodigo.ParserSQL.Reglas>
<add key="AnalisisSintactico" value="CalidadCodigo.ParserSQL.Reglas.AnalisisSintactico,CalidadCodigo.ParserSQL.AnalisisSintactico, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
<add key="AnalisisRecomendaciones" value="CalidadCodigo.ParserSQL.Reglas.AnalisisRecomendaciones,CalidadCodigo.ParserSQL.AnalisisRecomendaciones, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
<add key="FinFichero" value="CalidadCodigo.ParserSQL.Reglas.FinFichero,CalidadCodigo.ParserSQL.FinFichero, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
<add key="CheckTree" value="CalidadCodigo.ParserSQL.Reglas.CheckTreeException,CalidadCodigo.ParserSQL.FinFichero, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
<add key="AnalisisSintactico" value="CalidadCodigo.ParserSQL.Reglas.AnalisisSintactico,CalidadCodigo.ParserSQL.AnalisisSintactico, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
<add key="Regla1" value="CalidadCodigo.ParserSQL.Reglas.Regla1,CalidadCodigo.ParserSQL.Regla1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
<add key="Regla2" value="CalidadCodigo.ParserSQL.Reglas.Regla2,CalidadCodigo.ParserSQL.Regla2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
<add key="CheckTree" value="CalidadCodigo.ParserSQL.Reglas.CheckTreeException,CalidadCodigo.ParserSQL.FinFichero, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e" />
</Company.CalidadCodigo.ParserSQL.Reglas>
</configuration>
回答1:
Try group them into distinct values as follow:
XDocument doc = XDocument.Load(@"config.xml");
var query = from company in doc.Descendants("Company.CalidadCodigo.ParserSQL.Reglas")
from keys in company.Descendants()
group keys by keys.Attribute("value").Value into distinctResult
select new { key = distinctResult.FirstOrDefault().Attribute("key").Value, value = distinctResult.Key };
foreach (var result in query)
Console.WriteLine("key: " + result.key);
OUTPUT:
key: AnalisisSintactico
key: AnalisisRecomendaciones
key: FinFichero
key: CheckTree
key: Regla1
key: Regla2
回答2:
Following gives you the distinct rows.
XDocument doc = new XDocument(new XElement("doc"
, new XElement("add", new XAttribute("value", "11"))
, new XElement("add", new XAttribute("value", "23"))
, new XElement("add", new XAttribute("value", "22"))
, new XElement("add", new XAttribute("value", "22"))
, new XElement("add", new XAttribute("value", "22"))
, new XElement("add", new XAttribute("value", "11"))));
//Select Distinct Rows, gives 11,23,22
var result =
from row in doc.Descendants("add")
group row by (string)row.Attribute("value") into g
select g.First();
来源:https://stackoverflow.com/questions/4085065/xml-linq-removing-duplicate-nodes-in-xelement-c-sharp