I was writing an application and encountered a build error. The error was: "Error: Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlElement'".
An XmlElement is an XmlNode, but not all XmlNodes are XmlElements. If we look to the Inheritance Hierarchy (.NET 3.5) of the Base Class Library we see this:
- System.Object
- System.Xml.XmlNode
- System.Xml.XmlLinkedNode
- System.Xml.XmlElement
- System.Xml.XmlLinkedNode
- System.Xml.XmlNode
The XmlElement is more customized because an element is more specialized. A node can be many things like a document (complete XmlDocument). Basically, an XmlNode can contain a complete DOM. Where the XmlElement is more specialized and represents only an element.
You can easily convert an XmlNode to an XmlElement like this:
XmlElement myElement = (XmlElement)myXmlNode;
No comments:
Post a Comment