0

I am trying to read position from an xml file in unity, to use it to create and place objects in a scene.

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Xml.Serialization;
using System.IO;

here is the namespace created by the website "xml2csharp"

namespace Xml2CSharp
{
    [XmlRoot(ElementName = "position")]
    public class Position
    {
        [XmlAttribute(AttributeName = "x")]
        public float X { get; set; }
        [XmlAttribute(AttributeName = "y")]
        public float Y { get; set; }
        [XmlAttribute(AttributeName = "z")]
        public float Z { get; set; }
    }

    [XmlRoot(ElementName = "rotation")]
    public class Rotation
    {
        [XmlAttribute(AttributeName = "x")]
        public float X { get; set; }
        [XmlAttribute(AttributeName = "y")]
        public float Y { get; set; }
        [XmlAttribute(AttributeName = "z")]
        public float Z { get; set; }
    }

        [XmlRoot(ElementName = "positionCasqueAudio")]
        public class PositionCasqueAudio
    {
        [XmlElement(ElementName = "position")]
        public Position Position { get; set; }
        [XmlElement(ElementName = "rotation")]
        public Rotation Rotation { get; set; }
    }

    [XmlRoot(ElementName = "tailleStandard")]
    public class TailleStandard
    {
        [XmlAttribute(AttributeName = "x")]
        public string X { get; set; }
        [XmlAttribute(AttributeName = "y")]
        public string Y { get; set; }
        [XmlAttribute(AttributeName = "z")]
        public string Z { get; set; }
    }


    [XmlRoot(ElementName = "audio")]
    public class Audio
    {
        [XmlElement(ElementName = "position")]
        public Position Position { get; set; }
        [XmlElement(ElementName = "rotation")]
        public Rotation Rotation { get; set; }
        [XmlAttribute(AttributeName = "source")]
        public string Source { get; set; }
    }

    [XmlRoot(ElementName = "objetIdentifiable")]
    public class ObjetIdentifiable
    {
        [XmlElement(ElementName = "position")]
        public Position Position { get; set; }
        [XmlElement(ElementName = "rotation")]
        public Rotation Rotation { get; set; }
        [XmlElement(ElementName = "tailleStandard")]
        public TailleStandard TailleStandard { get; set; }
        [XmlElement(ElementName = "audio")]
        public List<Audio> Audio { get; set; }
        [XmlAttribute(AttributeName = "idObj")]
        public string IdObj { get; set; }
    }

    [XmlRoot(ElementName = "objetsIdentifies")]
    public class ObjetsIdentifies
    {
        [XmlElement(ElementName = "objetIdentifiable")]
        public List<ObjetIdentifiable> ObjetIdentifiable { get; set; }
    }

    [XmlRoot(ElementName = "scene3dSonore")]
    public class Scene3dSonore
    {
        [XmlElement(ElementName = "positionCasqueAudio")]
        public PositionCasqueAudio PositionCasqueAudio { get; set; }
        [XmlElement(ElementName = "objetsIdentifies")]
        public ObjetsIdentifies ObjetsIdentifies { get; set; }
    }

}

Here is the class where i try to link values in my xml file to my floats, and trying to read xml

public class LectureXML : MonoBehaviour
{
    Xml2CSharp.Position pos = new Xml2CSharp.Position();
    Xml2CSharp.Rotation rot = new Xml2CSharp.Rotation();
    Xml2CSharp.PositionCasqueAudio posCasque = new Xml2CSharp.PositionCasqueAudio();
    Xml2CSharp.Audio audio = new Xml2CSharp.Audio();
    Xml2CSharp.ObjetIdentifiable idientifiable = new Xml2CSharp.ObjetIdentifiable();
    Xml2CSharp.ObjetsIdentifies idientifie = new Xml2CSharp.ObjetsIdentifies();
    Xml2CSharp.Scene3dSonore scene3dson = new Xml2CSharp.Scene3dSonore();
    Xml2CSharp.TailleStandard taillestandard = new Xml2CSharp.TailleStandard();

this part doesnt work :

    Xml2CSharp.Position deserializedXML = new Xml2CSharp.Position();
    XMLSerializer serializer = new XMLSerializer(typeof(Xml2CSharp.Position));



    using (FileStream stream = File.OpenRead(@"XMLScene3D"))
    {
        deserializedXML=(Xml2CSharp.Position)serializer.Deserialize(Stream);
    }

I expect to get my x y z position and store it in a float that i'll can use later.

If you want the xml file i can join it

Error showed when the mouse is on File.opread

  • Why doesn't it work? Is there an error thrown? If so, please [edit](https://stackoverflow.com/posts/55356614/edit) your post to add it – MindSwipe Mar 26 '19 at 12:01
  • The change the type in Rotation class to decimal. The serializer will automatically parse the string in xml to a decimal. – jdweng Mar 26 '19 at 12:48
  • i tryed an other solution found here https://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – Muspellheim Mar 28 '19 at 14:09

0 Answers0