0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnityでXmlを読み込む

Posted at

使用環境

・Unity 2017.1.1f1
・VisualStudio2017


今回読み込むファイル「sample.xml」

<?xml version="1.0" encoding="UTF-8"?>
<リスト>
  <データ>
    <名前 言語="日本語">橋本</商品>
    <売上>120</売上>
  </データ>
  <データ>
   <名前 言語="日本語">山田</商品>
    <売上>10</売上>
  </データ>
  <データ>
    <名前 言語="日本語">田中</商品>
    <売上>1500</売上>
  </データ>
</リスト>

コード

LoadXML
using UnityEngine;

using System.Xml.Linq  //重要

public class LoadXML : MonoBehaviour
{
        void Start () {
                LoadData();
        }

        private void LoadData{
                //ディレクトリ指定してファイルを読み込み
                XDocument xml = XDocument.Load(@"C:\sample.xml");

                //テーブルを読み込む
                XElement table = xml.Element("リスト");

                //データの中身すべてを取得
                var rows = table.Elements("データ");

                //取り出し
                foreach (XElement row in rows)
                {
                    XElement item = row.Element("名前");
                    Debug.Log(item.Value);
                }
       }

}

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?