public virtual XmlComparisonState Analyze(XmlComparison comparison)
        {
            var state = XmlComparisonState.Different;

            switch (comparison.ComparisonType)
            {
            case XmlComparisonType.Doctype:
            case XmlComparisonType.NodeListSequence:
            case XmlComparisonType.NamespacePrefix:
            case XmlComparisonType.Encoding:
                state = XmlComparisonState.Similar;
                break;

            case XmlComparisonType.NodeType:
                var expectedNode = comparison.ExpectedDetails.Node as XText;
                var actualNode   = comparison.ActualDetails.Node as XText;

                if (expectedNode != null && actualNode != null)
                {
                    var expectedText = XmlComparer.GetNormalizedText(expectedNode.Value);
                    var actualText   = XmlComparer.GetNormalizedText(actualNode.Value);

                    if (expectedText == actualText)
                    {
                        state = XmlComparisonState.Similar;
                    }
                }

                break;
            }

            return(state);
        }
Beispiel #2
0
        public XmlComparisonResult CompareXml(string actualXml, string expectedXml, params string[] ignoreFields)
        {
            var comparer = new netbike.XmlComparer
            {
                NormalizeText = true,
                Analyzer      = NetBike.XmlUnit.XmlAnalyzer.Custom()
                                .SetEqual(NetBike.XmlUnit.XmlComparisonType.NodeListSequence)
                                .SetSimilar(NetBike.XmlUnit.XmlComparisonType.NamespacePrefix)
                                //Handler = ComparisonHandler
            };

            netbike.XmlComparisonResult result = comparer.Compare(expectedXml, actualXml);

            bool          isEqual   = true;
            List <String> errorList = null;

            if (!result.IsEqual)
            {
                errorList = new List <string>();

                foreach (var item in result.Differences)
                {
                    XElement elem = item.Difference.ActualDetails.Node as XElement;
                    if (elem != null && !elem.Name.LocalName.ContainsOneOf(ignoreFields))
                    {
                        StringBuilder sb = new StringBuilder();

                        if (item.Difference.ComparisonType == XmlComparisonType.NodeList)
                        {
                            sb.Append("Should Be [" + item.Difference.ExpectedDetails.Node);
                            sb.Append("] But Was [" + item.Difference.ActualDetails.Node + "]");
                        }
                        else if (item.Difference.ComparisonType == XmlComparisonType.TextValue)
                        {
                            sb.Append(item.Difference.ActualDetails.XPath + " Should Be ["
                                      + item.Difference.ExpectedDetails.Value);
                            sb.Append("] But Was [" + item.Difference.ActualDetails.Value + "]");
                        }
                        else if (item.Difference.ComparisonType == XmlComparisonType.NodeListLookup)
                        {
                            sb.Append(item.Difference.ActualDetails.XPath + " was not found in Actual value");
                        }
                        else
                        {
                            sb.Append("State: " + item.Difference.ComparisonType);
                            sb.Append(" Comparison: " + item.Difference);
                            sb.Append(Environment.NewLine);
                            isEqual = false;
                        }
                        sb.Append(Environment.NewLine);
                        errorList.Add(sb.ToString());
                        isEqual = false;
                    }
                }
            }


            return(new XmlComparisonResult()
            {
                AreEqual = isEqual, Errors = errorList
            });
        }
        public XmlComparisonResult CompareXml(string actualXml, string expectedXml, params string[] ignoreFields)
        {
            var comparer = new netbike.XmlComparer
            {
                NormalizeText = true,
                Analyzer = NetBike.XmlUnit.XmlAnalyzer.Custom()
                    .SetEqual(NetBike.XmlUnit.XmlComparisonType.NodeListSequence)
                    .SetSimilar(NetBike.XmlUnit.XmlComparisonType.NamespacePrefix)
                //Handler = ComparisonHandler

            };

            netbike.XmlComparisonResult result = comparer.Compare(expectedXml, actualXml);

            bool isEqual = true;
            List<String> errorList = null;

            if (!result.IsEqual)
            {
                errorList = new List<string>();

                foreach (var item in result.Differences)
                {
                    XElement elem = item.Difference.ActualDetails.Node as XElement;
                    if (elem != null && !elem.Name.LocalName.ContainsOneOf(ignoreFields))
                    {
                        StringBuilder sb = new StringBuilder();

                        if(item.Difference.ComparisonType == XmlComparisonType.NodeList)
                        {

                            sb.Append("Should Be [" + item.Difference.ExpectedDetails.Node);
                            sb.Append("] But Was [" + item.Difference.ActualDetails.Node + "]");

                        }
                        else if (item.Difference.ComparisonType == XmlComparisonType.TextValue)
                        {
                            sb.Append(item.Difference.ActualDetails.XPath +  " Should Be ["
                                + item.Difference.ExpectedDetails.Value);
                            sb.Append("] But Was [" + item.Difference.ActualDetails.Value + "]");
                        }
                        else if (item.Difference.ComparisonType == XmlComparisonType.NodeListLookup)
                        {
                            sb.Append(item.Difference.ActualDetails.XPath + " was not found in Actual value");
                        }
                        else
                        {
                            sb.Append("State: " + item.Difference.ComparisonType);
                            sb.Append(" Comparison: " + item.Difference);
                            sb.Append(Environment.NewLine);
                            isEqual = false;
                        }
                        sb.Append(Environment.NewLine);
                        errorList.Add(sb.ToString());
                        isEqual = false;
                    }

                }
            }

            return new XmlComparisonResult() { AreEqual = isEqual, Errors = errorList };
        }