Checking and returning Xml from a byte array I have a byte array in a C# program.

I want to determine as quickly as possible if the content is Xml. If it's the case, I also want to return the Xml itself.

By now I have this method :

    protected bool TryGetXElement(byte[] body, out XElement el)
    {
        el = null;
        // if there is no data, this is not xml :)
        if (body == null || body.Length == 0)
        {
            return false;
        }

        try
        {
            // Load the data into a memory stream
            using (var ms = new MemoryStream(body))
            {
                using (var sr = new StreamReader(ms))
                {
                    // if the first character is not &lt;, this can't be xml
                    var firstChar = (char)sr.Peek();
                    if (firstChar != '&lt;')
                    {
                        return false;
                    }
                    else
                    {
                        // ultimately, we try to parse the XML
                        el = XElement.Load(sr);
                        return true;
                    }
                }
            }
        }
        catch
        {
            // if it failed, we suppose this is not XML
            return false;
        }
    }


Is there potential improvement?
Why not verify if a root node exists rather than check for a '&lt;' ?

    public bool GetRootNode(XmlReader reader)
    {
        bool isValid;
        try
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    isValid = true;
                    break;
                }
            }
        }
        catch (XmlException x)
        {
            throw new XmlException(x.Message);
        }

        return isValid;
    }


You could also use the chain of responsibilities pattern to determine which parser you want to use.  

EDIT

Not sure if this will work, but here is an example on using the above code:

protected bool TryGetXElement(byte[] body, out XElement el)
{
    el = null;
    // if there is no data, this is not xml :)
    if (body == null || body.Length == 0)
    {
        return false;
    }

    try
    {
        // Load the data into a memory stream
        using (var ms = new MemoryStream(body))
        {
            using (var sr = new StreamReader(ms))
            {
                XmlReaderSettings settings = XmlReaderSettings { CheckCharacters = true; };
                using(XmlReader reader = XmlReader.Create(ms, settings))
                {
                    if (!GetRootNode(reader))
                    {
                        return false;
                    }
                    else
                    {
                        // ultimately, we try to parse the XML
                        el = XElement.Load(sr);
                        return true;
                    }
                }
            }

        }
    }
    catch
    {
        // if it failed, we suppose this is not XML
        return false;
    }
}