First time using basic XML

Hey guys!

This is my first time using XML and parsing it from my project and was wondering if there was something I was missing here!

Basically I have a video player that will play a video when a level loads IF the first child of the node is a video.

I’m not sure the best way to achieve this. It doesn’t seem like it wants to allow me to do a null check on the specific node since the node doesn’t actually exist for some levels. It did work if I passed the levels with no videos an “Empty” url but I don’t like this solution!

Here is my basic C# for handling the XML:

string filepath = Application.dataPath + @"/Files/GAMEdata.xml";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList levels = xmlDoc.GetElementsByTagName("Level");

        foreach(XmlNode level in levels)
        {
            if(level.Attributes["id"].Value == currentLevel.ToString())
            {
                print ("the current level ID is: " + level.Attributes["id"].Value);

                if(level.FirstChild.Attributes["url"].Value != "EMPTY")
                {
                    vid.playVideo(level.FirstChild.Attributes["url"].Value, true);
                }
                else
                {
                    print ("there are no videos");
                    bootLevel();
                }

            }


        }

Here is a snippet from my XML:

<?xml version="1.0"?>
<Levels>


    <Level id="0">
        <Video url="file://X:/Unity Projects/xmlstuff/files/introcutscene.mp4"></Video>
    </Level>
   
    <Level id="1">
        <Video url="EMPTY"></Video>
    </Level>
   
   
</Levels>

So what I have works but I don’t like it!

Thanks guys!

Why don’t you like it?

What is the value of level.FirstChild.Attributes[“url”].Value when there is no url attribute? Could you do

If(level.FirstChild.Attributes[“url”].Value)
{
// it exists
}

Okay! I made it much more modular! Now it creates a local xmlnode which I can do a null check on! So basically if it comes back null it will just load the level, otherwise it will play whatever the url attribute contains!

string filepath = Application.dataPath + @"/Files/GAMEdata.xml";
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filepath);
        XmlNodeList levels = xmlDoc.GetElementsByTagName("Level");

        foreach(XmlNode level in levels)
        {
            if(level.Attributes["id"].Value == currentLevel.ToString())
            {
                print ("the current level ID is: " + level.Attributes["id"].Value);

                XmlNode introvid = level.SelectSingleNode("IntroVideo");
                if(introvid != null)
                {
                    vid.playVideo(introvid.Attributes["url"].Value, true);
                }
                else
                {
                    print ("THERE IS NO VIDEO");
                    bootLevel();
                }
            }
        }

@Brathnann I didn’t like before how I had to always have the intro video as the first node and how for levels that didn’t have cutscenes I was manually typing in EMPTY.