Unity 讀取xml

朋丶Peng發表於2024-06-17

ReadXml.cs內容:

Unity 讀取xml
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

public class ReadXml : MonoBehaviour
{
    private XmlDocument xmldoc;
    private XmlNode root;
    private string url;

    void Start()
    {
        InitAccount();
        InitCases();
    }
    void InitAccount()
    {
        url = Application.streamingAssetsPath + "/Account_config.xml";
        xmldoc = new XmlDocument();
        xmldoc.Load(url);
        root = xmldoc.SelectSingleNode("root");

        ReadAccount();
    }
    void ReadAccount()
    {
        /*XmlNode account = root.SelectSingleNode("account");
        XmlElement ele = (XmlElement)account;
        Debug.Log(ele.GetAttribute("Username") + " " + ele.GetAttribute("Password"));*/

        /*XmlNode usernameNode = root.SelectSingleNode("username");
        Debug.Log(((XmlElement)usernameNode).InnerText);
        XmlElement passwordEle = (XmlElement)root.SelectSingleNode("password");
        Debug.Log(passwordEle.InnerText);*/

        foreach (var item in root.ChildNodes)
        {
            XmlNode xmlNode = item as XmlNode;
            Debug.Log(xmlNode.Name + "" + xmlNode.InnerText);
        }
    }
    void InitCases()
    {
        url = Application.streamingAssetsPath + "/Cases_config.xml";
        xmldoc = new XmlDocument();
        xmldoc.Load(url);
        root = xmldoc.SelectSingleNode("casesList");

        ReadCases();
    }
    void ReadCases()
    {
        foreach (var item1 in root.ChildNodes)
        {
            XmlNode xmlNode1 = item1 as XmlNode;

            foreach (var item2 in xmlNode1.ChildNodes)
            {
                XmlNode xmlNode2 = item2 as XmlNode;
                Debug.Log(xmlNode2.Name + "" + xmlNode2.InnerText);
            }
        }
    }
}
View Code

Cases_config.xml內容:

Unity 讀取xml
<?xml version="1.0" encoding="utf-8"?>
<casesList>
    <case>
        <name>張三三</name>
        <gender>男</gender>
        <age>32</age>
        <chiefComplaint>間斷呼吸困難2年,加重伴下肢水腫4天。</chiefComplaint>
        <historyOfPpresentIllness>患者於3天前疑似因發熱服用布洛芬出現便血,暗紅色,稀爛便,1天5次,每次約200ml,中途嘔血1次,鮮紅色,血中有胃內容物,約300ml,伴肚臍上方腹脹。患者面色蒼白,有頭暈、乏力、口渴,有心悸、反酸,尿量減少。無口腔潰瘍、頭痛、意識不清,無咳嗽、咳痰、氣促,無胸悶、胸痛,無燒心、噯氣、裡急後重。患者為求進一步診治,於1天前來我院急診就診,行“血常規”,示“Hb 103g/L”,擬“消化道出血”收入我科。患者自起病以來,胃納、精神差,睡眠可,大小便如上所述,體重體力無明顯變化。</historyOfPpresentIllness>
        <pastHistory>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</pastHistory>
    </case>
    <case>
        <name>李四四</name>
        <gender>男</gender>
        <age>34</age>
        <chiefComplaint>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</chiefComplaint>
        <historyOfPpresentIllness>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</historyOfPpresentIllness>
        <pastHistory>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</pastHistory>
    </case>
</casesList>
View Code

Account_config.xml內容:

Unity 讀取xml
<?xml version="1.0" encoding="utf-8"?>
<root>
    <!--<account Username="admin" Password="123456" />-->
    <username>admin</username>
    <number>007</number>
    <department>科室1</department>
    <group>組1</group>
    <gender>男</gender>
    <role>管理員</role>
    <password>123456</password>
</root>
View Code

相關文章