簡體   English   中英

如何反序列化此XML文檔?

[英]How to deserialize this XML document?

如何使用C#XmlSerializer反序列化此XML文檔結構?

...
<Data>
    <Hotspot>
        <Properties>
            <xPos>0.5</xPos>
            <yPos>0.3</yPos>
            <alpha>0.1</alpha>
        </Properties>
        <Properties>
            <xPos>0.4</xPos>
            <yPos>0.7</yPos>
            <alpha>0.2</alpha>
        </Properties>
    </Hotspot>
    <Hotspot>
        <Properties>
            <xPos>0.1</xPos>
            <yPos>0.2</yPos>
            <alpha>0.9</alpha>
        </Properties>
        <Properties>
            <xPos>0.2</xPos>
            <yPos>0.3</yPos>
            <alpha>0.8</alpha>
        </Properties>
    </Hotspot>
</Data>

我在Unity中工作,我想用此XML文檔中的位置數據來驅動GUI元素。

這是我反序列化的代碼:

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

public class Data
{
    public List<Hotspot> Hotspot;

    [XmlIgnore]
    public Data XmlData;

    public void Deserialize()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(Data));
        TextReader reader = new StreamReader(@"D:\Unity Projects\Axion800_Kabine\Assets\hspositions.xml");
        object obj = deserializer.Deserialize(reader);

        XmlData = (Data)obj;
        reader.Close ();
    }
}

public class Hotspot
{
    public List<Properties> Properties = new List<Properties>();
}

public class Properties
{
    public float xPos;
    public float yPos;

    public float alphaValue;
}

僅出於測試目的,我制作了另一個腳本來實際使用XML數據並將其附加到GUI元素:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class UseXMLData : MonoBehaviour 
{
    private RectTransform rectTrans;
    private int _index = 0;

    private Data data;

    void Awake () 
    {
        data = new Data();
        data.Deserialize();
        rectTrans = GetComponent<RectTransform>();
    }

    void Update () 
    {
        rectTrans.anchoredPosition = new Vector2(data.XmlData.Hotspot[0].Properties[_index].xPos, data.XmlData.Hotspot[0].Properties[_index].yPos);
        _index++;

        if(_index == data.XmlData.Hotspot[0].Properties.Count - 1)
            _index = 0;
    }
}

這是我收到的錯誤消息:

ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[Properties].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
UseXMLData.Update () (at Assets/UseXMLData.cs:21)

在我看來,從未成功讀取XML數據,因此數組不包含任何數據。 在設置Data,Hotspot和Properties類時,我做錯了什么嗎?

非常感謝您的幫助!

肖恩

如果您使用XmlElement屬性裝飾Hotspot和Properties集合,則反序列化可以正常工作:

public class Data
{
    [XmlElement("Hotspot")]
    public List<Hotspot> Hotspot;

    [XmlIgnore]
    public Data XmlData;

    public void Deserialize()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(Data));
        TextReader reader = new StreamReader(@"XMLFile1.xml");
        object obj = deserializer.Deserialize(reader);

        XmlData = (Data)obj;
        reader.Close();
    }
}

public class Hotspot
{
    [XmlElement("Properties")]
    public List<Properties> Properties = new List<Properties>();
}

public class Properties
{
    public float xPos;
    public float yPos;

    [XmlElement("alpha")]
    public float alphaValue;
}

在您的XML中

<alpha>???</alpha>

在你班上

public float alphaValue;

也許使用

public float alpha;

將工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM