private bool CompareChildren(XElement src, XElement dst, XmlCompareResults compareResults) { bool compareValue = true; foreach (XElement srcChild in src.Elements()) { IXmlIdentity xmlIdentity = GetXmlIdentity(srcChild); XElement dstChild = dst.Elements().Where(x => xmlIdentity.IsSelf(x, srcChild)).FirstOrDefault(); if (dstChild == null) { compareResults.AddError(new ElementNotExistError(src, dst, srcChild)); compareValue = false; } } return compareValue; }
private bool Compare(XElement src, XElement dst, XmlCompareResults compareResults) { if (!CompareAttributes(src, dst, compareResults) || !CompareAttributes(dst, src, compareResults)) return false; if (!CompareChildren(src, dst, compareResults) || !CompareChildren(dst, src, compareResults)) return false; foreach (XElement srcChild in src.Elements()) { IXmlIdentity xmlIdentity = GetXmlIdentity(srcChild); XElement dstChild = dst.Elements().Where(x => xmlIdentity.IsSelf(x, srcChild)).FirstOrDefault(); Compare(srcChild, dstChild, compareResults); } return true; }
private bool CompareAttributes(XElement src, XElement dst, XmlCompareResults compareResults) { bool compareValue = true; foreach (XAttribute srcAttr in src.Attributes()) { XAttribute dstAttr = dst.Attributes(srcAttr.Name).FirstOrDefault(); if (null == dstAttr) { compareResults.AddError(new AttributeNotExist(src, dst, srcAttr.Name)); compareValue = false; } else if (srcAttr.Value != dstAttr.Value) { compareResults.AddError(new AttributeNotEqual(src, dst, srcAttr.Name)); compareValue = false; } } return compareValue; }
public XmlCompareResults Compare(string xmlSource, string xmlTagert) { XElement src = XElement.Parse(xmlSource); XElement dst = XElement.Parse(xmlTagert); var compareResults = new XmlCompareResults(); if (src.Name != dst.Name) { compareResults.AddError(new TagDifferenceError(src, dst, "未找到指定元素")); return compareResults; } Compare(src, dst, compareResults); return compareResults; }