Пример #1
0
        private static void CompareXmlNodes(XmlComparisonRules rules, string pathA, XmlNode nodeA, string pathB, XmlNode nodeB)
        {
            if (rules.IsIgnoredPath(pathA))
            {
                return;
            }

            if (rules.IsIgnoredPath(pathB))
            {
                return;
            }

            if ((nodeA == null) && (nodeB == null))
            {
                return;
            }
            if ((nodeA == null) || (nodeB == null))
            {
                throw new Exception("[" + pathA + "] (nodeA == null) || (nodeB == null)");
            }

            if (nodeA.GetType() != nodeB.GetType())
            {
                throw new Exception("[" + pathA + "] nodeA.GetType() != nodeB.GetType()");
            }

            // generic node comparison
            CompareXmlNodeNames(pathA, nodeA, nodeB);
            CompareXmlNodeTypes(pathA, nodeA, nodeB);
            CompareXmlAttrColls(rules, pathA + "attr:", nodeA.Attributes, nodeB.Attributes);
            if (nodeA.HasChildNodes || nodeB.HasChildNodes)
            {
                CompareXmlNodeLists(rules, pathA, nodeA.ChildNodes, pathB, nodeB.ChildNodes);
            }
            else
            {
                // no children - just compare value
                if (String.Compare(nodeA.Value, nodeB.Value, true) != 0)
                {
                    // values are textually different but could be numerically equal
                    DateTimeOffset dtoA;
                    DateTimeOffset dtoB;
                    Int32          intA;
                    Int32          intB;
                    Decimal        numA;
                    Decimal        numB;
                    if (BothAreDateTimes(nodeA.Value, nodeB.Value))
                    {
                        if (!CompareDateTimes(DateTimeStyles.None, nodeA.Value, nodeB.Value) &&
                            !CompareDateTimes(DateTimeStyles.AssumeLocal, nodeA.Value, nodeB.Value) &&
                            !CompareDateTimes(DateTimeStyles.AssumeUniversal, nodeA.Value, nodeB.Value) &&
                            !CompareDateTimes(DateTimeStyles.NoCurrentDateDefault, nodeA.Value, nodeB.Value) &&
                            !CompareWithDateTimeZoneParser(nodeA.Value, nodeB.Value, false) &&
                            !CompareWithDateTimeZoneParser(nodeA.Value, nodeB.Value, true)
                            )
                        {
                            // date/times are not equal!
                            throw new Exception(String.Format("[{0}] NodeValue (as DateTime) '{1}' != '{2}'", pathA, nodeA.Value, nodeB.Value));
                        }
                    }
                    else if (DateTimeOffset.TryParse(nodeA.Value, out dtoA) && DateTimeOffset.TryParse(nodeB.Value, out dtoB))
                    {
                        if (dtoA != dtoB)
                        {
                            throw new Exception(String.Format("[{0}] NodeValue (as DateTimeOffset) '{1}' != '{2}'", pathA, dtoA, dtoB));
                        }
                    }
                    else if (Int32.TryParse(nodeA.Value, out intA) && Int32.TryParse(nodeB.Value, out intB))
                    {
                        if (intA != intB)
                        {
                            throw new Exception(String.Format("[{0}] NodeValue (as Int32) '{1}' != '{2}'", pathA, intA, intB));
                        }
                    }
                    else if (Decimal.TryParse(nodeA.Value, out numA) && Decimal.TryParse(nodeB.Value, out numB))
                    {
                        if (Math.Round(numA, 12) != Math.Round(numB, 12))
                        {
                            throw new Exception(String.Format("[{0}] NodeValue (as Decimal) '{1}' != '{2}'", pathA, numA, numB));
                        }
                    }
                    else
                    {
                        throw new Exception(String.Format("[{0}] NodeValue '{1}' != '{2}'", pathA, nodeA.Value, nodeB.Value));
                    }
                }
            }

            // specific node type comparisons
            if (nodeA.GetType() == typeof(XmlElement))
            {
                //CompareXmlElements(ignorePaths, path + "elt:" + nodeA.Name, nodeA as XmlElement, nodeB as XmlElement);
            }
            else if (nodeA.GetType() == typeof(XmlDeclaration))
            {
                //CompareXmlDeclarations(ignorePaths, path + "decl:" + nodeA.Name, nodeA as XmlDeclaration, nodeB as XmlDeclaration);
            }
            else if (nodeA.GetType() == typeof(XmlText))
            {
                CompareXmlTexts(rules, pathA + "text:" + nodeA.Name, nodeA as XmlText, nodeB as XmlText);
            }
            else
            {
                throw new NotImplementedException("[" + pathA + "] NodeType: " + nodeA.GetType().Name);
            }
        }
Пример #2
0
        private static void CompareXmlAttrColls(XmlComparisonRules rules, string path, XmlAttributeCollection a, XmlAttributeCollection b)
        {
            if (rules.IsIgnoredPath(path))
            {
                return;
            }

            if ((a == null) && (b == null))
            {
                return;
            }

            SortedDictionary <string, XmlAttribute> listA = FilterAndSortAttrColl(path, a);
            SortedDictionary <string, XmlAttribute> listB = FilterAndSortAttrColl(path, b);

            // get all keys
            SortedSet <string> attrKeys = new SortedSet <string>();

            foreach (var key in listA.Keys)
            {
                attrKeys.Add(key);
            }
            foreach (var key in listB.Keys)
            {
                attrKeys.Add(key);
            }

            // compare attrs
            foreach (string attrKey in attrKeys)
            {
                XmlAttribute attrA;
                if (!listA.TryGetValue(attrKey, out attrA))
                {
                    continue;
                }

                XmlAttribute attrB;
                if (listB.TryGetValue(attrKey, out attrB))
                {
                    // both exist - compare
                    CompareXmlAttribute(rules, path + AttrSortKey(attrA) + "/", attrA, attrB);
                }
                else
                {
                    // attrB is missing - might be optional or have a default value
                    if (rules.IsOptionalAttribute(path, attrKey))
                    {
                        // allowed to be missing
                        continue;
                    }
                    else
                    {
                        XmlDefaultAttribute defaultAttribute;
                        if (rules.TryGetDefaultAttribute(path, attrKey, out defaultAttribute))
                        {
                            CompareXmlAttribute(rules, path + AttrSortKey(attrA) + "/", attrA, defaultAttribute);
                        }
                        else
                        {
                            // missing and no default value - oops!
                            throw new Exception(String.Format("[{0}] Emitted Attribute '{1}' missing!", path, attrKey));
                        }
                    }
                }
            }
        }