it leader
article thumbnail









XML 데이터 불러오기 / 저장하기




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
using System.Xml;
 
public class RecItem
{
    public    string    Name;
    public    int        Level;
    public    float    Critical;
}
 
public sealed class ItemIO
{
    public static void Write(List<RecItem> ItemList, string filePath)
    {
        XmlDocument Document = new XmlDocument();
        XmlElement ItemListElement = Document.CreateElement("ItemList");
        Document.AppendChild(ItemListElement);
 
        foreach(RecItem Item in ItemList)
        {
            XmlElement ItemElement = Document.CreateElement("Item");
            ItemElement.SetAttribute("Name", Item.Name);
            ItemElement.SetAttribute("Level", Item.Level.ToString());
            ItemElement.SetAttribute("Critical", Item.Critical.ToString());
            ItemListElement.AppendChild(ItemElement);
        }
        Document.Save(filePath);
    }
 
    public static List<RecItem> Read(string filePath)
    {
        XmlDocument Document = new XmlDocument();
        Document.Load(filePath);
        XmlElement ItemListElement = Document["ItemList"];
 
        List<RecItem> ItemList = new List<RecItem>();
 
        foreach(XmlElement ItemElement in ItemListElement.ChildNodes)
        {
            RecItem Item = new RecItem();
            Item.Name = ItemElement.GetAttribute("Name");
            Item.Level = System.Convert.ToInt32(ItemElement.GetAttribute("Level"));
            Item.Critical = System.Convert.ToSingle(ItemElement.GetAttribute("Critical"));
            ItemList.Add(Item);
        }
        return ItemList;
    }
}
 
cs






데이터 저장하기 사용하기 


"ItemList" 요소를 만들고 XML Document에 넣었다. List로 받은 데이터를 itemList에 갯수만큼 넣을 준비를 한다. 요소 "Item" 하나 만듭니다. 값은 문자열로만 저장이 가능하므로 ToString()을 사용한다. 숫자로 된 자료는 이런식으로 저장한다. 그리고 완성이 된 아이템은 "ItemList"에 담다. for문이 끝나고 나면 저장 한다.

사용의 예)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class WriteTest : MonoBehaviour
{
    void Start()
    {
        List<RecItem> itemList = new List<RecItem>();
 
        for(int i = 0; i < 100++i)
        {
            RecItem item = new RecItem();
            item.Name = "아이템";
            item.Level = 1;
            item.Critical = Random.Range(0.1f, 1.0f);
            itemList.Add(item);
        }
        ItemIO.Write(itemList, Application.dataPath + "/Output/ItemList_Attributes.xml");
    }
}
 
 
cs








데이터 불러오기 사용하기 


XML형식으로 저장된 파일을 불러온다. 불러온 문서의 "ItemList";에 해당하는 요소를 가져온다. ( C#에서는 문자열을 Index화 시키기 때문에 가능한다 ) RecItem 자료형 리스트를 만듭니다. 이제 for문을 돌면서 XML Document로부터 RecItem List로 값을 가져 온다. 문자열이 아닌 데이터는 System.Convert.ToInt32 , System.Convert.ToSingle 를 이용하여 변환한다. (정수, 실수)

사용의 예)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class ReadTest : MonoBehaviour
{
    void Start()
    {
        List<RecItem> itemList = ItemIO.Read(Application.dataPath + "/Output/ItemList_Attributes.xml");
        for(int i = 0; i < itemList.Count; ++i)
        {
            RecItem item = itemList[i];
            Debug.Log(string.Format("Item[{0}] : ({1}, {2}, {3})",
                i, item.Name, item.Level, item.Critical,));
        }
    }
}
 
 
cs






profile

it leader

@dev__pixui

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!