在 C# 中查找高性能的 XyDiff 端口(带有 XID 的 XML Diff Patch)
本文关键字:XID 带有 XML Diff Patch 端口 查找 高性能 XyDiff | 更新日期: 2023-09-27 18:36:44
我将一些数据存储为 XML,并将用户的更改存储为与原始 XML 的差异,因此可以在运行时修补用户的数据。
原始 xml 的示例(仅其中的一部分):
<module id="">
<title id="">
...
</title>
<actions>
...
</actions>
<zones id="" selected="right">
<zone id="" key="right" name="Right" />
</zones>
</module>
用户差异示例(用户从右到左更改了所选的值):
<xd:xmldiff version="1.0" srcDocHash=""
options="IgnoreChildOrder IgnoreNamespaces IgnorePrefixes IgnoreSrcValidation "
fragments="no"
xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
<xd:node match="1">
<xd:node match="3">
<xd:change match="@selected">left</xd:change>
</xd:node>
</xd:node>
</xd:xmldiff>
问题是,修补程序会查找 XML 节点的顺序。如果顺序发生变化,则无法再应用差异,甚至更糟的是,它将错误地应用。所以我更喜欢通过 XID 进行修补。有谁知道 XyDiff 的 C# 性能库或算法?
我们现在开发了自己的解决方案,运行良好。
我们做了什么:
- 确保原始文件中的每个 XML 节点都具有唯一的 ID(否)在哪个层面上)
- 为用户更改生成平面 XML 修补程序,该修补程序仅保存每个更改节点的更改(无级别结构)
- 如果用户更改了值,请在修补程序 XML 中写入 XMLw 节点属性目标 ID 指向原始节点的 ID
- 如果用户更改了属性,请使用xmlpatch 节点的值
- 如果用户更改了某个值,请将该值写入 XMLw 补丁节点
xml 修补程序如下所示:
<patch>
<xmlpatch sortorder="10" visible="true" targetId="{Guid-x}" />
<xmlpatch selected="left" targetId="{Guid-y}" />
<xmlpatch targetId="{Guid-z}">true</xmlpatch>
</patch>
生成补丁xml的代码非常简单。我们遍历所有 xml 节点,并针对每个节点遍历所有属性。如果节点的属性或值与原始节点不同,我们将使用该属性或值生成补丁节点。请注意,密码是在一个晚上写好的;)
public static XDocument GenerateDiffGram(XDocument allUserDocument, XDocument runtimeDocument)
{
XDocument diffDocument = new XDocument();
XElement root = new XElement("patch");
AddElements(root, runtimeDocument, allUserDocument.Root);
diffDocument.Add(root);
return diffDocument;
}
private static void AddElements(XElement rootPatch, XDocument runtimeDocument, XElement allUserElement)
{
XElement patchElem = null;
if (allUserElement.Attribute("id") != null
&& !string.IsNullOrWhiteSpace(allUserElement.Attribute("id").Value))
{
// find runtime element by id
XElement runtimeElement = (from e in runtimeDocument.Descendants(allUserElement.Name)
where e.Attribute("id") != null
&& e.Attribute("id").Value.Equals(allUserElement.Attribute("id").Value)
select e).FirstOrDefault();
// create new patch node
patchElem = new XElement("xmlpatch");
// check for changed attributes
foreach (var allUserAttribute in allUserElement.Attributes())
{
XAttribute runtimeAttribute = runtimeElement.Attribute(allUserAttribute.Name);
if (!allUserAttribute.Value.Equals(runtimeAttribute.Value))
{
patchElem.SetAttributeValue(allUserAttribute.Name, runtimeAttribute.Value);
}
}
// check for changed value
if (!allUserElement.HasElements
&& !allUserElement.Value.Equals(runtimeElement.Value))
{
patchElem.Value = runtimeElement.Value;
}
}
// loop through all children to find changed values
foreach (var childElement in allUserElement.Elements())
{
AddElements(rootPatch, runtimeDocument, childElement);
}
// add node for changed value
if (patchElem != null
&& (patchElem.HasAttributes
|| !string.IsNullOrEmpty(patchElem.Value)))
{
patchElem.SetAttributeValue("targetId", allUserElement.Attribute("id").Value);
rootPatch.AddFirst(patchElem);
}
}
在运行时,我们将保存在补丁 xml 中的更改修补回来。我们按 targetid 处理原始节点并覆盖属性和值。
public static XDocument Patch(XDocument runtimeDocument, XDocument userDocument, string modulePath, string userName)
{
XDocument patchDocument = new XDocument(userDocument);
foreach (XElement element in patchDocument.Element("patch").Elements())
{
// get id of the element
string idAttribute = element.Attribute("targetId").Value;
// get element with id from allUserDocument
XElement sharedElement = (from e in runtimeDocument.Descendants()
where e.Attribute("id") != null
&& e.Attribute("id").Value.Equals(idAttribute)
select e).FirstOrDefault();
// element doesn't exist anymore. Maybe the admin has deleted the element
if (sharedElement == null)
{
// delete the element from the user patch
element.Remove();
}
else
{
// set attributes to user values
foreach (XAttribute attribute in element.Attributes())
{
if (!attribute.Name.LocalName.Equals("targetId"))
{
sharedElement.SetAttributeValue(attribute.Name, attribute.Value);
}
}
// set element value
if (!string.IsNullOrEmpty(element.Value))
{
sharedElement.Value = element.Value;
}
}
}
// user patch has changed (nodes deleted by the admin)
if (!patchDocument.ToString().Equals(userDocument.ToString()))
{
// save back the changed user patch
using (PersonalizationProvider provider = new PersonalizationProvider())
{
provider.SaveUserPersonalization(modulePath, userName, patchDocument);
}
}
return runtimeDocument;
}