Why doesn't Selection.activeObject return a TextAsset?

I’m just writing a simple XML well-formed utility and Selection.activeObject isn’t passing back TextAsset that I can grab the text content from. Why is this the case? It would seem fairly intuitive for it to function in this way.

Am I missing something? Are TextAssets only for runtime?

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Xml;
using System.IO;

public class XMLUtility : Editor {

    [MenuItem("Assets/xml/Check XML File")]
    private static void CheckXMLFile()
    {
        //Selection.activeObject
       
        string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
        //hdrpath = Path.ChangeExtension(filepath,".xml");
        //string extension  = Path.GetExtension(filepath);
        Debug.Log("XMLUtility:" + filepath);
        Debug.Log("XMLUtility:" + Selection.activeObject.GetType());
       
        //TextAsset ta = Selection.activeObject as TextAsset;
        TextAsset ta = AssetDatabase.LoadMainAssetAtPath(filepath) as TextAsset;
       
       
        if (ta != default(TextAsset))
        {
            Test(ta.text);
        }
        else
        {
            Debug.LogWarning("XMLUtility: No TextAsset selected");
        }
       
    }
   
    static void Test(string xml) {
        using (XmlReader xr = XmlReader.Create(
            new StringReader(xml))) {
            try {
                while (xr.Read()) { }
                Debug.Log("XMLUtility: XML Is Valid");
            } catch (Exception ex) {
                Debug.LogError("XMLUtility: XML is not Valid: " + ex.Message);
            }
        }
    }
   
    // Note that we pass the same path, and also pass "true" to the second argument.
    [MenuItem("Assets/xml/Check XML File", true)]
    private static bool CheckXMLFileValidation()
    {
        // This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).
        if ( Selection.activeObject.GetType() == typeof(DefaultAsset))
        {
            string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
            string extension  = Path.GetExtension(filepath);
            //Debug.Log("XMLUtility: CheckXMLFileValidation" + extension);
            if (extension == ".xml") {
                return true;
            }   
        }
        return false;
    }

}

This is a real problem that nobody has really resolved, as far as I know. I’ve searched for days on the correct way to get the text of an InputField, and even saw others complaining of this, but I guess this is not a priority at the moment.

What exactly isn’t working for you? i just tried your code, and except for the wrong validating method, it works fine. Selection.activeObject does hold a reference to the selected TextAsset and you can use it. Also, activeObject already holds the actual loaded object, there’s no need to load that again:

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Xml;
using System.IO;

public class XMLUtility : Editor {

    [MenuItem("Assets/xml/Check XML File")]
    private static void CheckXMLFile()
    {
        string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);

        Debug.Log("XMLUtility:" + filepath);
        Debug.Log("XMLUtility:" + Selection.activeObject.GetType());

        TextAsset ta = Selection.activeObject as TextAsset;

        if (ta != null)
        {
            Test(ta.text);
        }
        else
        {
            Debug.LogWarning("XMLUtility: No TextAsset selected");
        }
    }

    static void Test(string xml) {
        using (XmlReader xr = XmlReader.Create(
            new StringReader(xml))) {
            try {
                while (xr.Read()) { }
                Debug.Log("XMLUtility: XML Is Valid");
            } catch (Exception ex) {
                Debug.LogError("XMLUtility: XML is not Valid: " + ex.Message);
            }
        }
    }

    // Note that we pass the same path, and also pass "true" to the second argument.
    [MenuItem("Assets/xml/Check XML File", true)]
    private static bool CheckXMLFileValidation()
    {
            string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
            string extension  = Path.GetExtension(filepath);
           
        if (extension == ".xml") {
                return true;
        }

        return false;
    }
}