我无法使用C#脚本统一读取xml文件

时间:2019-03-26 11:58:25

标签: c# xml

我正试图统一读取xml文件中的位置,以使用它在场景中创建和放置对象。

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

这是网站“ 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; }
    }

}

这是我尝试将xml文件中的值链接到我的float并尝试读取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();

这部分不起作用:

    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);
    }

我希望得到x y z的位置并将其存储在一个浮点数中,以备日后使用。

如果您想要xml文件,我可以加入它

Error showed when the mouse is on File.opread

0 个答案:

没有答案