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 9242519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:35:44+00:00 2026-06-18T08:35:44+00:00

I know, there are tons of Post for this topic, but since I read

  • 0

I know, there are tons of Post for this topic, but since I read two days during the posts and nothing worked, I decided to ask here.
I have a XML (Level01.xml) file like this

<map version="1.0">
 <tileset>
  <image source="....>
 </tileset>
 <layer name="background">
     <data encoding="csv">
     3,3,3,3,3,3,3,
     3,3,3,3,3,3,3,
     3,3,3,3,3,3,3,
     3,3,3,3,3,3,3
     </data>
 </layer>
 <layer name="walls">
     <data encoding="csv">
     182,182,182,182,182,8,8,
     182,8,182,8,8,8,8,
     182,182,182,182,182,8,8,
     182,8,182,8,8,8,8,
     </data>
 </layer>
</map>

Its a (tile-)map with different layers and I want to draw it in XNA. Therefore I want to read the data from each layer into a string[]
This is what I tried, but it doesn’t work for the second string[] walldata and I am absolutely desperate because I don’t understand why

public class LevelXmlReader
{
    public string[] backgroundData;
    public string[] wallData;

    LevelXmlReader()
    {
        XDocument doc = XDocument.Load(@"Level/Level01.xml");
        foreach (XElement layer in doc.Element("map").Descendants("layer"))
        {

            var lay = doc.Element("map").Element("layer");
            var layName = lay.Attribute("name").Value;
            switch (layName)
            {
                case "background":
                    {
                        backgroundData = lay.Element("data").Value.Split(',');
                    }
                    break;
                case "walls":
                    {
                        wallData = lay.Element("data").Value.Split(',');
                    }
                    break;
            }

        }

    }

}

When I try to draw wallData, there is always the ExceptionError “Object reference not set to an instance of an object.”

  • 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-06-18T08:35:45+00:00Added an answer on June 18, 2026 at 8:35 am

    inside your loop you are declaring

    var lay = doc.Element("map").Element("layer");
    

    this is unecessary, because you already HAVE the individual layer. you need to remove that line entirely and use the layer variable from the foreach loop. The inside of the loop should now look like this

    foreach (XElement layer in doc.Element("map").Descendants("layer"))
    {
        switch (layer.Attribute("name").Value)
        {
            case "background":       
                backgroundData = layer.Element("data").Value.Split(','); 
                break;
    
            case "walls":
                wallData = layer.Element("data").Value.Split(',');
                break;
        }
    }
    

    In your code, you’re always grabbing the same layer both times, instead of stepping through them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's tons of other threads around about this same topic, but none
I know there are tons of answers for this topic, but couldn't find the
I know there are tons of similar questions and answers for this topic, but
I know there are tons of threads regarding this issue but I have not
I know there is a lot on this topic but I can't get any
I know there have been tons of answers on this subject, but I still
I know there are TONS of questions on this website regarding this topic, as
I know there's tons of threads about this. And I read a few of
So I know there are tons of questions similar to this but I havent
OK, I know there are tons of questions like this, but for some reason

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.