using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace Aneef.XMLCompare { class XmlCompare { XmlDocument output = new XmlDocument(); XmlNode properties; /// /// Compares 2 XML and returns an output xml of updated properties /// Developed by: Aneef Fashir /// Blog: http://www.aneef.net /// Date : 10/09/2008 /// /// Existing Property XML /// New Objects Property XML /// XMLDocument Document public XmlDocument compare(string existing, string Updated) { XmlDocument currentXML = new XmlDocument(); currentXML.LoadXml(existing); XmlDocument updatedXML = new XmlDocument(); updatedXML.LoadXml(Updated); XmlNode cProperty = currentXML.SelectSingleNode("Properties").CloneNode(true); XmlNode nProperty = updatedXML.SelectSingleNode("Properties").CloneNode(true); prepareOutput(cProperty.CloneNode(false)); try { foreach (XmlNode child in cProperty.ChildNodes) { foreach (XmlNode uChild in nProperty.ChildNodes) { if (child.OuterXml == uChild.OuterXml) { addNode(child); } else { if (child.Attributes["Name"].Value == uChild.Attributes["Name"].Value) { updateMatchingName(child.InnerXml, uChild); } } } } foreach (XmlNode updnode in nProperty.ChildNodes) { bool isExists = false; foreach (XmlNode mynode in properties.ChildNodes) { if (mynode.Attributes["Name"].Value == updnode.Attributes["Name"].Value) { isExists = true; } } if (isExists == false) { addNode(updnode); } } return output; } catch (Exception ex) { throw ex; } } /// /// Prepare Parent Properties Node /// /// XMLNode ParentNode private void prepareOutput(XmlNode parentnode) { try { properties = output.CreateElement("Properties"); addAttributes(ref properties, parentnode); output.AppendChild(properties); } catch (Exception ex) { throw ex; } } /// /// Add Attributes of updated nodes to the output node /// /// XMLNode output /// XMLNode existingnode private void addAttributes(ref XmlNode outputNode,XmlNode ExistingNode) { try { XmlAttribute[] atribs = new XmlAttribute[ExistingNode.Attributes.Count]; ExistingNode.Attributes.CopyTo(atribs, 0); foreach (XmlAttribute atb in atribs) { XmlAttribute bt = outputNode.OwnerDocument.CreateAttribute(atb.Name); bt.Value = atb.Value; outputNode.Attributes.Append(bt); } } catch (Exception ex) { throw ex; } } /// /// Add an entire node from existing to the new one /// /// XMLNode Output private void addNode(XmlNode outputnode) { try { XmlNode property = output.CreateElement(outputnode.Name); addAttributes(ref property, outputnode); property.InnerXml = outputnode.InnerXml; properties.AppendChild(property); } catch (Exception ex) { throw ex; } } /// /// Update Nodes which their Name attributes values are equal. /// element value will be taken from exisiting node, and the atttributes will be taken from the new node. /// /// Existing Nodes Element value /// XMLNode Updated Node private void updateMatchingName(string innerValue, XmlNode node) { try { XmlNode property = output.CreateElement(node.Name); addAttributes(ref property, node); property.InnerXml = innerValue; properties.AppendChild(property); } catch (Exception ex) { throw; } } } }