Am I loading this xml file correctly?

I use C # to load an XML file, but I don’t think I am doing it in the best way, I would really appreciate if someone would either check how I am doing this, or point me in the right direction to do this it's better.

I am only posting some of what I am doing to download the XML file, but I use this technique for everything,

XDocument doc = XDocument.Load(xmldir);
var layers = doc.Document.Descendants("Layer");
foreach (var layer in layers)
{
    if (layer.Attribute("Name").Value == "Terrain")
    {
        LoadTerrain(layer);
    }
}

// The load terrain function:
public void LoadTerrain(XElement elements)
{
    var items = elements.Descendants("Items");
    var element = elements.Descendants("CustomProperties").Descendants("Property");

    float tileSize = 0;
    string tileTexture = "";
    foreach (var property in element)
    {
        if (property.Attribute("Name") != null)
        {
            string name = property.Attribute("Name").Value;
            if (name == "TileSize")
            {
                tileSize = float.Parse(property.Attribute("Description").Value);
            }
            else if (name == "Texture")
            {
                tileTexture = property.Attribute("Description").Value;
            }
        }
    }

    PhysicShape shape = new PhysicShape(world, JabActor.BodyType.STATIC);
    shape.TextureDir = tileTexture;
    shape.TileSize = tileSize;
    shape.Initialize();
    foreach (var item in items)
    {
        var parts = item.Descendants("Item");

        foreach (var part in parts)
        {
            if (part.Name == "Item")
            {
                element = part.Descendants("Position");
                float posx = float.Parse(element.Elements("X").ElementAt(0).Value);
                float posy = float.Parse(element.Elements("Y").ElementAt(0).Value);

                ////element = part.Descendants("Rotation");
                float rot = float.Parse(part.Elements("Rotation").ElementAt(0).Value);

                element = part.Descendants("Scale");
                float width = float.Parse(element.Elements("X").ElementAt(0).Value);
                float height = float.Parse(element.Elements("Y").ElementAt(0).Value);


                shape.AddRectangle(new Vector3(posx, -posy, 0), new Vector2(width, height), rot);
            }
        }
    }

    shape.FinalizeVertices();
    AddNode(shape);
}

Here's the XML file: http://medsgames.com/levelg.xml

+3
source share
3 answers

, - XML. XSD, # xsd.exe SDK, XML ~ 3 . .

+3

, . IXmlSerializable /, , xml.

xsd.exe IXmlSerializable.

+2

XML #. : XML

+1

Source: https://habr.com/ru/post/1781817/


All Articles