C# – How to get the XML root node with C#

cxml

I know that it's possible to get any XML node using C# if you know the node name, but I want to get the root node so that I can find out the name. Is this possible?

Update: I'm using XMLTextReader to read in the URL of a file and then loading that into XMLDocument object. Basically I'm trying to avoid LINQ to XML, but if there's another, better way then I'm always a good student.

Best Answer

Root node is the DocumentElement property of XmlDocument

XmlElement root = xmlDoc.DocumentElement

If you only have the node, you can get the root node by

XmlElement root = xmlNode.OwnerDocument.DocumentElement