在C#中寻找高性能的XyDiff端口(带有XID的XML Diff Patch)

时间:2012-06-11 16:04:32

标签: c# xml xmldiff

我将一些数据存储为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#的高性能库或算法?

1 个答案:

答案 0 :(得分:2)

我们现在开发了自己的解决方案,工作正常。

我们做了什么:

  • 确保原始文件中的每个xml节点都具有唯一ID(否) 关于哪个级别的事情)
  • 生成一个平面xml补丁,用于仅保存的用户更改 每个更改节点的更改(没有级别结构)
  • 如果用户更改了值,请在补丁xml中编写xmlpatch节点 属性targetid指向原始节点的id
  • 如果用户更改了属性,请使用new编写属性 值到xmlpatch节点
  • 如果用户更改了值,请将值写入xmlpatch节点

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;
}