I am working in an older piece of code so I do not have the option to use XDocument.I am trying to replace one XmlNode with another but from some reason XmlDocument.ReplaceChild() complains that: The node to be removed is not a child of this node.I don't understand why I am getting this error because I am getting reference to the XmlDocument from the element to be replaced.\[code\]private XmlNode ReplaceWithSpan(XmlNode node){ if (XmlNodeType.Element == node.NodeType) { XmlDocument ownerDocument = node.OwnerDocument; XmlNode spanNode = ownerDocument.CreateElement("span"); for (int i = 0; i < node.Attributes.Count; i++) { XmlAttribute attribute = node.Attributes; AddAttribute(spanNode, attribute.Name, attribute.Value); } ownerDocument.ReplaceChild(spanNode, node); //this doesn't work return spanNode; } throw new InvalidCastException(string.Format("Could not replace custom edit node of type {0}.", node.NodeType.ToString()));}\[/code\]Can someone where I've gone wrong?