Beispiel #1
0
        private void CompareAttributes(XElement node1, XElement node2, IXmlCompareHandler callback)
        {
            var attribs = node1.GetAttributes()
                          .Concat(node2.GetAttributes())
                          .GroupBy(a => a.Name.ToString())
                          .Select(a => a.First());

            foreach (var attrib in attribs)
            {
                // compare Attributes in both documents
                var attribute1 = node1.Attribute(attrib.Name);
                var attribute2 = node2.Attribute(attrib.Name);

                if (attribute1 == null && attribute2 != null)
                {
                    //added
                    callback.AttributeAdded(new AttributeAddedEventArgs(attribute2.GetXPath(), attribute2, attribute2.Parent.LineNumber()));
                    continue;
                }


                if (attribute1 != null && attribute2 == null)
                {
                    //removed
                    callback.AttributeRemoved(new AttributeRemovedEventArgs(attribute1.GetXPath(), attribute1, attribute1.Parent.LineNumber()));
                    continue;
                }


                if (attribute1 != null && attribute2 != null)
                {
                    //might have changed
                    //compare values

                    var val1 = attribute1.Value;
                    var val2 = attribute2.Value;

                    if (string.Equals(val1, val2))
                    {
                        continue;
                    }

                    callback.AttributeChanged(new AttributeChangedEventArgs(attribute1.GetXPath(), attribute1, attribute1.Parent.LineNumber(), attribute2, attribute2.Parent.LineNumber()));
                    continue;
                }

                throw new Exception("Invalid scenario while comparing Attributes: " + attrib);
            }
        }
        private void CompareAttributes(XElement node1, XElement node2, IXmlCompareHandler callback)
        {
            var attribs = node1.GetAttributes()
                .Concat(node2.GetAttributes())
                .GroupBy(a => a.Name.ToString())
                .Select(a => a.First());

            foreach (var attrib in attribs)
            {
                // compare Attributes in both documents
                var attribute1 = node1.Attribute(attrib.Name);
                var attribute2 = node2.Attribute(attrib.Name);

                if (attribute1 == null && attribute2 != null)
                {
                    //added
                    callback.AttributeAdded(new AttributeAddedEventArgs(attribute2.GetXPath(), attribute2, attribute2.Parent.LineNumber()));
                    continue;
                }


                if (attribute1 != null && attribute2 == null)
                {
                    //removed
                    callback.AttributeRemoved(new AttributeRemovedEventArgs(attribute1.GetXPath(), attribute1, attribute1.Parent.LineNumber()));
                    continue;
                }


                if (attribute1 != null && attribute2 != null)
                {
                    //might have changed
                    //compare values

                    var val1 = attribute1.Value;
                    var val2 = attribute2.Value;

                    if (string.Equals(val1, val2))
                        continue;

                    callback.AttributeChanged(new AttributeChangedEventArgs(attribute1.GetXPath(), attribute1, attribute1.Parent.LineNumber(), attribute2, attribute2.Parent.LineNumber()));
                    continue;
                }

                throw new Exception("Invalid scenario while comparing Attributes: " + attrib);
            }

        }