The OneStream Community is temporarily frozen until June 29th due to the ongoing maintenance. Please read the blog post here to learn more.
Forum Discussion
MarcusH
2 years agoValued Contributor
Format of XML files in Application extract zip file
Does anyone know if it is possible to format the XML files that are created by the application extract. In particular I want to format the Metadata extract so that there is a single line for the memb...
- 2 years ago
I get what you're doing. It makes sense. However, Git has the ability to track this without needing to mangle the XML. in VS you can open your xml in a diff to see the individual changes. Its built in. 🙂
RobbSalzmann
2 years agoValued Contributor II
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;
}
}
MarcusH
2 years agoValued Contributor
It's to use with Git. I wanted to know if there were any options to control the format of the XML to make the Git versioning simpler. Thanks for the code; it looks really good.
- RobbSalzmann2 years agoValued Contributor II
I get what you're doing. It makes sense. However, Git has the ability to track this without needing to mangle the XML. in VS you can open your xml in a diff to see the individual changes. Its built in. 🙂
Related Content
- 4 years ago
- 2 years ago