private bool CompareXml(string fn1, string fn2, string xmlType) { XmlDocument xmlDoc1, xmlDoc2; try { xmlDoc1 = new XmlDocument(); xmlDoc2 = new XmlDocument(); xmlDoc1.Load(fn1); xmlDoc2.Load(fn2);; CXmlFileNodeImp nd1, nd2; nd1 = new CXmlFileNodeImp(xmlType, fn1); nd2 = new CXmlFileNodeImp(xmlType, fn2); m_DuplicateKeyMemo.Clear(); IXmlSetting currentSetting = m_SettingFactory.ReadSettingCollection(m_ToolSetting.GetXmlSettingFilePath().Value) .GetSetting(xmlType); DomToNode(currentSetting, xmlDoc1.DocumentElement, ref nd1); m_DuplicateKeyMemo.Clear(); DomToNode(currentSetting, xmlDoc2.DocumentElement, ref nd2); if (nd1 == nd2) { return(true); } else { return(false); } } catch (Exception e) { throw e; } }
public static bool operator ==(CXmlFileNodeImp node1, CXmlFileNodeImp node2) { bool mattributesFlag, textFlag, childNodesFlag; #region compair the text of the node /********************************************************************************** * compair the text of the node **********************************************************************************/ textFlag = (node1.text == node2.text) ? true : false; if (!textFlag) { CLog loger = CLog.CreateInstance(); loger.Append(XmlSettingRptDicCategory.ChangedNodeText.ToString(), node2.name, node2.key, node2.xPath, "Inner Xml Text", node1.text, node2.text); } #endregion #region compair the attributes of the nodes /********************************************************************************** * compair the attributes of the node **********************************************************************************/ mattributesFlag = true; // Union the two Node m_mattributes by key, // if the value is different for the key, keep the first Node attribute IEnumerable <KeyValuePair <string, string> > attrUnion = node1.m_attributes. Union <KeyValuePair <string, string> >(node2.m_attributes, ProjectionEqualityComparer <KeyValuePair <string, string> > .Create(a => a.Key)); foreach (KeyValuePair <string, string> tempKeyValPair in attrUnion) { // find the new added attributes if (!node1.m_attributes.Keys.Any <string>((string temKey) => temKey == tempKeyValPair.Key)) { CLog loger = CLog.CreateInstance(); loger.Append(XmlSettingRptDicCategory.AddedAttribute.ToString(), node2.name, node2.key, node2.xPath, tempKeyValPair.Key, "[New Added]", tempKeyValPair.Value); mattributesFlag = false; } // find the deleted attribute and changed attributes if (!node2.m_attributes.Keys.Any <string>((string temKey) => temKey == tempKeyValPair.Key)) { //deleted attribute CLog loger = CLog.CreateInstance(); loger.Append(XmlSettingRptDicCategory.DeletedAttribute.ToString(), node2.name, node2.key, node2.xPath, tempKeyValPair.Key, tempKeyValPair.Value, "[Deleted]"); mattributesFlag = false; } else if (node2.m_attributes[tempKeyValPair.Key] != tempKeyValPair.Value) { //changed attribute CLog loger = CLog.CreateInstance(); loger.Append(XmlSettingRptDicCategory.ChangedAttribute.ToString(), node2.name, node2.key, node2.xPath, tempKeyValPair.Key, node1.m_attributes[tempKeyValPair.Key], node2.m_attributes[tempKeyValPair.Key]); mattributesFlag = false; } } #endregion #region compair the child nodes of the nodes childNodesFlag = true; IEnumerable <KeyValuePair <string, IXmlFileNode> > childUnion = node1.m_childNodes. Union <KeyValuePair <string, IXmlFileNode> >(node2.m_childNodes, ProjectionEqualityComparer <KeyValuePair <string, IXmlFileNode> > .Create(a => a.Key)); foreach (KeyValuePair <string, IXmlFileNode> tempKeyValPair in childUnion) { // find the new added child nodes if (!node1.m_childNodes.Keys.Any <string>((string temKey) => temKey == tempKeyValPair.Key)) { // new nodes CLog loger = CLog.CreateInstance(); loger.Append(XmlSettingRptDicCategory.AddedChildNode.ToString(), node2.name, node2.key, node2.xPath, "[Child Notes]", "[New Added]", tempKeyValPair.Key); childNodesFlag = false; } // find the deleted child nodes and changed child nodes if (!node2.m_childNodes.Keys.Any <string>((string temKey) => temKey == tempKeyValPair.Key)) { // deleted nodes CLog loger = CLog.CreateInstance(); loger.Append(XmlSettingRptDicCategory.DeletedChildNode.ToString(), node2.name, node2.key, node2.xPath, "[Child Notes]", tempKeyValPair.Key, "[Deleted]"); childNodesFlag = false; } else { CXmlFileNodeImp tmpNode1 = (CXmlFileNodeImp)tempKeyValPair.Value; CXmlFileNodeImp tmpNode2 = (CXmlFileNodeImp)node2.m_childNodes[tempKeyValPair.Key]; if (!(tmpNode1 == tmpNode2)) { childNodesFlag = false; } } } #endregion if (mattributesFlag && textFlag && childNodesFlag) { return(true); } else { return(false); } }
private void DomToNode(IXmlSetting currentSetting, XmlNode currentNode, ref CXmlFileNodeImp parent) { bool NodeCompareFlag = false; try { IXmlSettingNode settingNode = currentSetting.GetNode(currentNode.Name); if (settingNode != null) { NodeCompareFlag = settingNode.compare_flag; } } catch (Exception e) { // do nothing } if (NodeCompareFlag) { CXmlFileNodeImp neoNode = new CXmlFileNodeImp(currentNode.Name, GetXPath(currentNode)); string key = ""; if (GetKeyForNode(currentNode, neoNode.name, currentSetting, out key)) { neoNode.key = key; } key = neoNode.name + "|" + neoNode.key; if (currentNode.NodeType == XmlNodeType.Text) { neoNode.text = currentNode.Value; parent.m_childNodes.Add(neoNode.key, neoNode); neoNode.m_parentNodes.Add(parent.key, parent); return; } foreach (XmlAttribute xmlAttr in currentNode.Attributes) { List <string> listOfAttrName = new List <string>(); GetEffectiveAttributeNames(currentNode, neoNode.name, currentSetting, out listOfAttrName); if (listOfAttrName.IndexOf(xmlAttr.Name) >= 0) { neoNode.m_attributes.Add(xmlAttr.Name, xmlAttr.Value); } } foreach (XmlNode childNode in currentNode.ChildNodes) { DomToNode(currentSetting, childNode, ref neoNode); } if (parent.m_childNodes.ContainsKey(key)) { // dealing with duplicate key, add numeric suffix to the key if (m_DuplicateKeyMemo.ContainsKey(key)) { m_DuplicateKeyMemo[key] = m_DuplicateKeyMemo[key] + 1; } else { m_DuplicateKeyMemo.Add(key, 1); } key = key + "." + m_DuplicateKeyMemo[key].ToString(); } parent.m_childNodes.Add(key, neoNode); neoNode.m_parentNodes.Add(key, parent); } }