Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6683179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:47:27+00:00 2026-05-26T04:47:27+00:00

I am currently working on a game project in XNA 4.0 we currently read

  • 0

I am currently working on a game project in XNA 4.0 we currently read our levels in by referencing .txt files (considered xml but .txt works fine) we are able to reference the levels as referenced files. as follows:

LevelScreen.cs:

    private void LoadLevel()
    {
        string levelPath;
        // possible case switch or counter for multiple levels

        string level1Path;
        level1Path = "GameContent\\levels\\level1.txt";
        string level2Path;
        level2Path = "GameContent\\Levels\\level2.txt";
        string level3Path;
        level3Path = "GameContent\\Levels\\level3.txt";

        //Loops to find levels
        while (true)
        {
            //finds level files using game location
            //levelPath = "Levels/level1.txt";
            //levelPath = Path.Combine(FullName, "Content/" + levelPath);
            //Will be fixed to load from wherever the games file is located to find the level files.

            //gets path of executable
            levelPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

            if (levelPath.EndsWith("Game\\bin\\x86\\Debug\\Game.exe"))
            {
                //deletes end of path to set filepath to project folder
                levelPath = levelPath.Remove(levelPath.Length - 43);
                //increments level everytime it is loaded
                levelNum++;
                //keeps level within first and last
                if (levelNum > lastLevel)
                    levelNum = 1;
                //adds filepath for level
                switch (levelNum)
                {
                    case 1:
                        levelPath = string.Concat(levelPath, level1Path);
                        break;
                    case 2:
                        levelPath = string.Concat(levelPath, level2Path);
                        break;
                    case 3:
                        levelPath = string.Concat(levelPath, level3Path);
                        break;
                 //HERE we can put in a case statement to load other levels.
                   default:  // currently no action (?win screen?)
                        break;
                }
            }

            if (File.Exists(levelPath))
                break;
        }

MenuScreen.cs:

        private void GenerateLevelSelectMenu()
        {
        List<string> Levels = new List<string>();
        string directory = "Content/Levels";

        //get list of files in levelsFolder
        foreach (string file in Directory.GetFiles(directory))
        {
            Levels.Add(file);
        }

        //generate XML file.
        string targetDirectory = "Content/Menus/LevelSelect.xml";
        using (StreamWriter writer = new StreamWriter(targetDirectory, false))
        {
            //needed to be read as xml
            writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");

            //writing xml
            writer.WriteLine("<Menu>");
            writer.WriteLine("  <MenuName>Level Select</MenuName>");
            //stepping through the list of Levels to generate the data
            for (int ii = 0; ii < Levels.Count(); ii++)
            {
                writer.WriteLine("  <MenuItem>");
                writer.WriteLine("    <MenuItemText>" + Levels[ii] + "</MenuItemEvent>");
                writer.WriteLine("    <MenuItemEvent>" + Levels[ii] + "</MenuItemEvent>");
                writer.WriteLine("    <EventParams>Option" + ii + "</EventParams>");
                writer.WriteLine("  </MenuItem>");
            }
            //needed to go to the previous menu.
            writer.WriteLine("  <MenuItem>");
            writer.WriteLine("    <MenuItemText>Back</MenuItemEvent>");
            writer.WriteLine("    <MenuItemEvent>BackEvent</MenuItemEvent>");
            writer.WriteLine("    <EventParams>OptionBack</EventParams>");
            writer.WriteLine("  </MenuItem>");
            //placement of the menu itself
            writer.WriteLine("  <PositionX>427</PositionX>");
            writer.WriteLine("  <PositionY>240</PositionY>");
            writer.WriteLine("  <SelectedItemNum>0</SelectedItemNum>");
            writer.WriteLine("</Menu>");
            writer.Close();
        }
    }

Output to file(LevelSelect.xml):

<?xml version="1.0" encoding="utf-8" ?>
<Menu>
  <MenuName>Level Select</MenuName>
  <MenuItem>
    <MenuItemText>Content/Levels\level1.txt</MenuItemEvent>
    <MenuItemEvent>Content/Levels\level1.txt</MenuItemEvent>
    <EventParams>Option0</EventParams>
  </MenuItem>
  <MenuItem>
    <MenuItemText>Content/Levels\level2.txt</MenuItemEvent>
    <MenuItemEvent>Content/Levels\level2.txt</MenuItemEvent>
    <EventParams>Option1</EventParams>
  </MenuItem>
  <MenuItem>
    <MenuItemText>Content/Levels\level3.txt</MenuItemEvent>
    <MenuItemEvent>Content/Levels\level3.txt</MenuItemEvent>
    <EventParams>Option2</EventParams>
  </MenuItem>
  <MenuItem>
    <MenuItemText>Back</MenuItemEvent>
    <MenuItemEvent>BackEvent</MenuItemEvent>
    <EventParams>OptionBack</EventParams>
  </MenuItem>
  <PositionX>427</PositionX>
  <PositionY>240</PositionY>
  <SelectedItemNum>0</SelectedItemNum>
</Menu>

but the program is acting like all that is in the file is this:

<?xml version="1.0" encoding="utf-8" ?>
<Menu>
  <MenuName>Level Select</MenuName>
</Menu>

if even that

the next step is to create a level editor/generator, but before that I need to be able to get the files from the Level folder without using static strings. then through string manipulation hand that to the content manager to get the level to be loaded (whether it is developer or player created)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:47:28+00:00Added an answer on May 26, 2026 at 4:47 am

    Why not just enumerate the level directory? Am I missing something? Perhaps something like:

    static void CheckLevels(string directory) {
        List<string> levels = new List<string>();
    
        foreach (string file in Directory.GetFiles(directory, ".txt")) { // You could change ".txt" to some other file extension. I always think it's cool for my games to use special extensions =)
            levels.Add(file);
        }
    
        return levels;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on my 2D game project (Java), but so far any kind
I am currently working on a small personal multiplayer game project. I am using
So i am currently working on a simple game project that most people start
I'm currently working on a XNA game prototype. I'm trying to achieve a isometric
I'm currently working on a game with html5/js, using box2dweb for the collision but
I am currently working on a small landing page for my personal game project.
I'm working on an XNA game and I am using ViewPort.Project and ViewPort.Unproject to
I'm currently working on a game based on Slick2D, but I ran into a
Im currently working on a game that uses multi touch to apply zoom to
I'm currently working on a game and I wish to have a main menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.