Why are you wanting to use string mangling to compare differences in XML files? Better to use the XML libraries for this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
internal class XmlMetadataMemberComparer
{
public static void CompareXmlFiles(string filePath1, string filePath2)
{
XDocument doc1 = XDocument.Load(filePath1);
XDocument doc2 = XDocument.Load(filePath2);
var members1 = doc1.Descendants("member");
var members2 = doc2.Descendants("member");
foreach (var member1 in members1)
{
var memberName = member1.Attribute("name")?.Value;
var member2 = members2.FirstOrDefault(m => m.Attribute("name")?.Value == memberName);
if (member2 != null)
{
CompareMemberProperties(memberName, member1, member2);
}
}
}
private static void CompareMemberProperties(string memberName, XElement member1, XElement member2)
{
var properties1 = member1.Descendants("property").ToDictionary(p => p.Attribute("name")?.Value, p => p.Attribute("value")?.Value);
var properties2 = member2.Descendants("property").ToDictionary(p => p.Attribute("name")?.Value, p => p.Attribute("value")?.Value);
foreach (var prop1 in properties1)
{
if (properties2.TryGetValue(prop1.Key, out var value2) && prop1.Value != value2)
{
Console.WriteLine($"Difference found in member '{memberName}', property '{prop1.Key}': File1='{prop1.Value}', File2='{value2}'");
}
}
}
}
If you really must use string mangling, this should get you close:
internal class XmlMangler
{
public static void FlattenMemberTagContents(string inputFilePath, string outputFilePath)
{
string xmlContent = File.ReadAllText(inputFilePath);
string pattern = @"(<member.*?>)([\s\S]*?)(<\/member>)";
string formattedXml = Regex.Replace(xmlContent, pattern, SingleLineMember);
File.WriteAllText(outputFilePath, formattedXml);
}
private static string SingleLineMember(Match m)
{
string singleLine = Regex.Replace(m.Value, @"\s+", " ");
return singleLine;
}
}