C# 比较xml文件

C# 比较xml文件,c#,C#,我有一个C windows应用程序,在这里我试图比较xml的内容。 基于xml文档中的差异,我需要进一步处理。是否有内置的API来比较xml?不确定,但您可以反序列化这两个xml片段并对其执行对象操作。Microsoft xml Diff&Patch tool为此提供了API:可能再也没有人使用xml了,但是今晚我需要创建一些代码,在两个xml片段之间执行比较,除非元素在包含的列表中重复,否则不关心元素顺序。对于包含的列表,我确实希望确保元素保持其原始顺序,以确保不对其内容执行某种形式的意外排序

我有一个C windows应用程序,在这里我试图比较xml的内容。
基于xml文档中的差异,我需要进一步处理。是否有内置的API来比较xml?

不确定,但您可以反序列化这两个xml片段并对其执行对象操作。

Microsoft xml Diff&Patch tool为此提供了API:

可能再也没有人使用xml了,但是今晚我需要创建一些代码,在两个xml片段之间执行比较,除非元素在包含的列表中重复,否则不关心元素顺序。对于包含的列表,我确实希望确保元素保持其原始顺序,以确保不对其内容执行某种形式的意外排序

public static class XmlCompare
{
    /// <summary>
    /// Compares xml docs without regard to element order, unless the elements are a nested list
    /// </summary>
    /// <param name="primary">Any valid xml</param>
    /// <param name="secondary">Any valid xml</param>
    public static void Compare(string primary, string secondary)
    {
        Compare(XElement.Parse(primary), XElement.Parse(secondary));
    }

    /// <summary>
    /// Compares xml docs without regard to element order, unless the elements are a nested list
    /// </summary>
    /// <param name="primary">Any valid xml</param>
    /// <param name="secondary">Any valid xml</param>
    public static void Compare(XElement primary, XElement secondary)
    {
        // iterate attributes found in primary; compare values
        foreach (XAttribute primaryAttribute in primary.Attributes())
        {
            var secondaryAttribute = secondary.Attribute(primaryAttribute.Name.LocalName);
            if (secondaryAttribute == null)
            {
                throw new Exception($"Element '{primary.Name.LocalName}' attribute '{primaryAttribute.Name.LocalName}' missing from secondary xml");
            }
            else
            {
                var primaryValue = primaryAttribute.Value;
                var secondaryValue = secondaryAttribute.Value;
                if (primaryValue != secondaryValue)
                    throw new Exception($"Element '{primary.Name.LocalName}' attribute '{primaryAttribute.Name.LocalName}' has an unequal value.  Expected '{primaryValue}', actual '{secondaryValue}'");
            }
        }

        // iterate attributes found in secondary; just ensure all attributes found in the secondary exist in the primary
        foreach (var secondaryAttribute in secondary.Attributes())
        {
            if (primary.Attribute(secondaryAttribute.Name.LocalName) == null)
                throw new Exception($"Element '{secondary.Name.LocalName}' attribute '{secondaryAttribute.Name.LocalName}' missing from primary xml");
        }

        // iterate elements of primary; compare values
        for (var i = 0; i < primary.Elements().Count(); i++)
        {
            var primaryElement = primary.Elements().Skip(i).Take(1).Single();
            var elementOccursMultipleTimes = primary.Elements(primaryElement.Name.LocalName).Count() > 1;
            if (elementOccursMultipleTimes)
            {
                // this comparison fails if the sequence of element entries has been altered between the primary
                // and secondary document, such as sorting a list by name in one document, but sorting by date
                // in the other.
                var secondaryElement = secondary.Elements().Skip(i).Take(1).SingleOrDefault();
                if (secondaryElement == null)
                {
                    throw new Exception($"Element '{primaryElement.Name.LocalName}' missing from secondary xml");
                }

                Compare(primaryElement, secondaryElement);
            }
            else
            {
                var secondaryElement = secondary.Element(primaryElement.Name.LocalName);
                if (secondaryElement == null)
                    throw new Exception($"Element '{primaryElement.Name.LocalName}' missing from secondary xml");
                // to understand recursion, we must first understand recursion
                Compare(primaryElement, secondaryElement);
            }
        }

        // iterate elements of secondary; just ensure all the elements found in the secondary exist in the primary
        foreach (var secondaryElement in secondary.Elements())
        {
            if (primary.Element(secondaryElement.Name.LocalName) == null)
                throw new Exception($"Element '{secondaryElement.Name.LocalName}' missing from primary xml");
        }
    }

希望这对其他人有所帮助。

正如我在问题中提到的,我想要一个API。为什么你这么热衷于API。我认为做你工作的代码很好。亲爱的Srinivas Reddy Thatiparthy你试图在这里创建一个论点,我正在寻找的是API本身。如果我要编写这些实现,那么这个问题就不会放在首位。谢谢Jay,我认为这会很有用。看到API了吗