在Unity C中解析XML文件#

时间:2016-09-25 15:59:58

标签: c# xml unity3d xml-parsing

我在使用C#将XML文件解析为对象时遇到了一些问题。整个项目在Unity3D中。所以我有这个XML文件:

<Questions>
    <Question>
        <questionText>What is this?</questionText>
        <answer>blablabla</answer>
    </Question>
</Questions>

这是我的解析类:

using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System;

public struct Montage {
    [XmlElement("questionText")]
    public string questionText;

    [XmlElement("answer")]
    public string answer;
}

[XmlRoot("Questions"), XmlType("Questions")]
public class ConfigScene {

    [XmlArray("Questions")]
    [XmlArrayItem("Question")]
    public List<Montage> questions = new List<Montage> ();

    public static ConfigScene Load(string path) {
        try {
            XmlSerializer serializer = new XmlSerializer (typeof(ConfigScene));
            using(FileStream stream = new FileStream(path, FileMode.Open)) {
                 return serializer.Deserialize(stream) as ConfigScene;
            }
        } catch (Exception e) {
             UnityEngine.Debug.LogError ("Exception loading config file: " + e);

             return null;
        }
    }
}

我在Start()方法的相机对象中调用了这个“加载”方法:

void Start () {
    confScene = ConfigScene.Load(Path.Combine(Application.dataPath, "Config/config2.xml"));
    foreach(Montage o in confScene.questions) {
        Debug.Log (o.questionText);
    }
}

问题是我的问题列表是空的,我没有得到任何提供的数据。我做错了吗?也许有人之前做过并知道这段代码有什么问题?

2 个答案:

答案 0 :(得分:1)

XML文件架构( config2.xml )和相应类中的XML序列化属性不匹配。您的XML文档的根元素(Questions)和问题列表元素是冲突的。

将您的XmlRoot ElementName(现在是问题,代码中的第16行)更改为其他内容(例如 Root )。相应地更改 config2.xml 文件的内容:将整个文档括在Root元素中,如下所示。

<Root>
  <Questions>
    <Question>
      <questionText>What is this?</questionText>
      <answer>blablabla</answer>
    </Question>
  </Questions>
</Root>

反序列化现在应该没问题。

答案 1 :(得分:0)

我必须回答,因为我想在这里添加一些代码。所以现在我可以运行它并获得数据。但我的XML文件更嵌套。这是XML文件:

<?xml version="1.0" encoding="utf-8"?>

<MontageConfig>
    <Parameters>
        <Parameter>
            <mainObject>
                <name>tableTop</name>
            </mainObject>
            <connectors>
                <connector>
                    <name>bolt</name>
                    <count>4</count>
                </connector>
                <connector>
                    <name>spike</name>
                    <count>5</count>
                </connector>
            </connectors>
        </Parameter>
    </Parameters>
</MontageConfig>

我的班级:

using UnityEngine;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System;

public struct Connectors {
    [XmlElement("name")]
    public string connectorName;

    [XmlElement("count")]
    public int connectorCount;
}

public struct Montage
{
    [XmlElement("mainObject")]
    [XmlElement("name")]
    public string name;

    [XmlElement("connectors")]
    [XmlElement("connector")]
    public List<Connectors> connectors;

}

[XmlRoot("MontageConfig")]
public class ConfigScene
{

    [XmlArray("Parameters")]
    [XmlArrayItem("Parameter")]
    public List<Montage> questions = new List<Montage>();

    public static ConfigScene Load(string path)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ConfigScene));
            using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                //Debug.Log(serializer.Deserialize(stream) as ConfigScene);
                return serializer.Deserialize(stream) as ConfigScene;
            }
        }
        catch (Exception e)
        {
            UnityEngine.Debug.LogError("Exception loading config file: " + e);

            return null;
        }
    }
}

问题在于连接器。我想我没有做好。所以我无法获取此Connections对象的任何数据。你知道怎么解决这个问题吗?