Search code examples
c#xmlsap-gui

Error reading fieldelement of xml node (c#)


Locally, SAP stores the name of all installed systems in a xml file, placed in %appdata%/SAP/Common/SAPUiLandscape.xml.

The file looks like this (I had to modify the content, as it's confidential):

Simplified SAP Ini file in XML format

Now, I want to read all the service nodes with its subnodes using the following code (C#):

        public static System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>> GetIstalledSAPSystems()
    {
        System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>> SystemList = new System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>>();

        #region Getting folder path
        string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        AppDataPath = AppDataPath.Replace('\\', '/');
        if (AppDataPath.Substring(AppDataPath.Length - 1, 1) != "/") { AppDataPath += "/"; }
        AppDataPath += "SAP/Common/SAPUILandscapeMod.xml";
        #endregion

        System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
        xdoc.Load(AppDataPath);

        foreach(System.Xml.XmlNode Node in xdoc.SelectNodes("/Landscape/Services/*"))
        {
            Console.WriteLine(Node.InnerText); //Returns emptry string??
            Console.WriteLine(Node["name"].Name); //ERROR: Object reference not set to an instance of an object
            Console.WriteLine(Node["name"].Value); //ERROR: Object reference not set to an instance of an object
        }

        return SystemList;
    }

I don't really understand that, because the subnode "name" is available in all elements and the InnerText shouldn't be empty. Does anybody have an idea, where I'm wrong? Thank you.


Solution

  • Node["name"] returns element "name" you should use Node.Attributes["name"]