Help with reading XML

Hi,

I’m trying to read an XML file to get information. I’m new to this technique, and I feel like I am probably doing is wrong. This is my code so far:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

public class GetItemDescription : MonoBehaviour {

    public static string GetDescription (string itemName) {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml("Descriptions.text");
        XmlNodeList descriptionsList = xmlDoc.GetElementsByTagName("items");

        foreach (XmlNode items in descriptionsList) {
            XmlNodeList itemContent = items.ChildNodes;
            
            foreach(XmlNode item in itemContent) {
                if (item.Value == itemName) {
                    string description = item.Value;

                    Debug.Log(description);
                    return description;
                }
            }
        }

        return "No description found";
    }
}

This is the XML file

items>
	<flashlight>hi</flashlight>
</items>

I want to get the name of the item to search, and get a description of it. Any guidance will be greatly appreciated! Thank you so much!

Well you aren’t really doing it wrong - but you can use XPath to write a query to get you back just certain parts of your document which saves writing code to parse the entire structure. I refer to this tutorial for XPath.