Leon Meijer's Weblog

About my personal life, technology and business/work related...
posts - 128, comments - 334, trackbacks - 0

From XmlNode to XmlElement and what's the difference

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

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;

Hope this helps.

Print | posted on Thursday, February 28, 2008 5:48 PM |

Powered by: