1

my problem is as follows: I want to parse a xml-file created with Tiled Map Editor where I inserted one objectlayer (for collision objects). But unfortunately, Tiled named the node in the xml-file "objectgroup" and its descendants "object"

<objectgroup name="solidObjects" width="100" height="100">
 <object gid="265" x="16" y="35"/>
 <object gid="265" x="66" y="36"/>
</objectgroup>

I am trying to do something like

XDocument doc = XDocument.Load("pathtoFile\sourcefile.xml");
List<Rectangle> objectList = new List<Rectangle>();

foreach (var object in doc.Element("objectgroup").Descendants("object"))
{ objectList.Add(objectRectangle); }

But since "object" is a protected word in c#, it doesn't work. Any tips how to handle this problem the easiest way?

3
  • 4
    Why not just choosing another variable name? Commented Mar 4, 2013 at 10:08
  • 1
    yeah - just call it obj or something Commented Mar 4, 2013 at 10:11
  • Oh, I thought the variable has to have the same name as the node Commented Mar 4, 2013 at 14:20

1 Answer 1

3

If you absolutely, definitely have to use object as your variable name (which is not recommended), then you can prefix it with an @ sign:

foreach (var @object in doc.Element("objectgroup").Descendants("object")) {
    objectList.Add(@object);
}

The @ sign prefix allows identifiers in your code to use reserved words as their names.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.