I got two folders, and each folder contains a certain amount of .xml files named differently. Each XML file contains one particular line which looks like this:
<general name = "something" ....... version = "something" .... >
Each folder has one .xml file that share the same name
, which has the value "something"
in the above example. However, their file name is different.
My goal is, for every file in each folder, to match them by their name in the .xml file and compare their version number.
CodePudding user response:
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication21
{
class Program
{
static string[] FOLDERS = {@"c:\temp\folder1\", @"c:\temp\folder2"};
static void Main(string[] args)
{
List<General> generals0 = new List<General>();
List<General> generals1 = new List<General>();
for (int i = 0; i < 2; i )
{
string folder = FOLDERS[i];
foreach (string file in Directory.GetFiles(folder, "*.Xml"))
{
XDocument doc = XDocument.Load(file);
XElement xGeneral = doc.Descendants("general").FirstOrDefault();
if (xGeneral != null)
{
string name = (string)xGeneral.Attribute("name");
string version = (string)xGeneral.Attribute("version");
if (name != null)
{
General general = new General();
switch(i)
{
case 0 :
generals0.Add(general);
break;
case 1 :
generals1.Add(general);
break;
}
general.filename = file;
general.name = name;
general.version = version;
}
}
}
}
//join to lists
var results = (from g0 in generals0
join g1 in generals1 on g0.name equals g1.name
select new { g0 = g0, g1 = g1 }
).ToList();
}
}
public class General
{
public string filename { get; set; }
public string name { get; set; }
public string version { get; set; }
}
}
CodePudding user response:
In XSLT 3.0, do
<xsl:template name="xsl:initial-template">
<xsl:variable name="one"
select="map:merge(collection('folder1?select=*.xml')
//general ! map{@name : string(@version)})"/>
<xsl:variable name="two"
select="map:merge(collection('folder2?select=*.xml')
//general ! map{@name : string(@version)})"/>
<xsl:variable name="combined"
select="map:merge(($one, $two),
map{'duplicates': 'combine'})"/>
<xsl:for-each select="map:keys($combined)">
name={.} : versions={$combined?.}
</xsl:for-each>
</xsl:template>