首页 > 解决方案 > 在 Unity 中使用 C# 解析包含条件语句的 XML 元数据

问题描述

我有一个 XML 文件,其中充满了这样的事件,其中包含多个要检查的语句。

这个动作怎么叫?我迷路了,我什至不知道从哪里开始寻找。

<BENEvents>

  <BENDescisionQ1 flagBEN_00="true" flagBEN_01="true" flagBEN_28="true" flagBEN_29="false">
    <AskQuestions caption='Looking today again at you glass with milk you decide....'>
        <Question event='DescisionQ1Yes'>Cowcostumes look great.</Question>
        <Question event='DescisionQ1No'>Flatnesss is justice, so NO</Question>
    </AskQuestions>
  </BENDescisionQ1>

  <BENDescisionQ2 ....>
    .....
  </BENDescisionQ2>

<BENEvents>

请帮助指出我正确的方向。

编辑:答案解决了我的问题,thatnk you

这就是我的代码现在如何处理更一般的事件。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using System.IO;

public class mllhildTestLinq : MonoBehaviour
{
    public mllhildTestController controller;

    public void QueryXML(string filename, string parentNode, GameObject listForDisplay)//(string[] args)
    {
        List<GeneralEvent> listEvents = new List<GeneralEvent>();
        GeneralEvent GeneralEvents = new GeneralEvent();

        listEvents = GeneralEvents.parse(filename, parentNode);

        // just printing for testing
        //Debug.Log("Is this shit getting here?");
        int i = 0;
        foreach (GeneralEvent newEvent in listEvents)
        {
            //Debug.Log(i);
            listForDisplay.GetComponent<PrefabListButtoms>().AddNewButton(newEvent.nodeName, listForDisplay);
            Debug.Log(newEvent.nodeName);
            i++;
            foreach (KeyValuePair<string, string> keyValuePair in newEvent.general)
            {
                Debug.Log(keyValuePair.Key + "   " + keyValuePair.Value);
            }
            //for (int i = 0; i < newEvent.general.Count; i++)
            //{
            //    controller.AddText("     key: " + newEvent.general[i].Key + "   value: " + newEvent.general[i].Value);
            //}
            //controller.AddText("\n");
        }

    }
}

public class GeneralEvent
{
    public static List<GeneralEvent> listEvents = new List<GeneralEvent>();
    public string nodeName { get; set; }
    public List<KeyValuePair<string, string>> general { get; set; }
    //string patternNumber = @"\d+$";
    string patternGeneral = @"[^\d]+[^\$]+"; 
    public List<GeneralEvent> parse(string filename, string parentNode)
    {
        listEvents.Clear();
        XElement generalEvents;
        XDocument doc = XDocument.Load(filename);
        try
        {
            generalEvents = doc.Descendants(parentNode).First();
            //generalEvents = doc.Elements(parentNode).FirstOrDefault();
        }
        catch
        {
            generalEvents = null;
        }


        //XElement generalEvents = doc.Descendants(parentNode).FirstOrDefault();
        if (generalEvents != null)
        {
            //Debug.Log("---------------------------------------------------------------------");
            foreach (XElement descision in generalEvents.Elements())
            {
                GeneralEvent newEvent = new GeneralEvent();
                listEvents.Add(newEvent);
                //Debug.Log(descision.Name);
                //   newEvent.nodeName = string.Parse(Regex.Match(descision.Name.LocalName, patternGeneral).Value);
                newEvent.nodeName = descision.Name.ToString();
                newEvent.general = descision.Attributes()
                   .Select(x => new KeyValuePair<string, string>(Regex.Match(x.Name.LocalName, patternGeneral).Value, (string)x)).ToList();
            }
        }
        else
        {
            Debug.Log("null");
        }

        return listEvents;
    }

}

标签: c#xmlunity3dxml-parsingunity2.0

解决方案


这不是一个简单的 Xml Parse。使用 Regex 查看我的 Xml Linq 解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            BenEvent benEvent = new BenEvent();
            benEvent.parse(FILENAME);
        }
    }
    public class BenEvent
    {
        public static List<BenEvent> listEvents = new List<BenEvent>();
        public int number { get; set; }
        public List<KeyValuePair<int, Boolean>> flags { get; set; }
        public string question { get; set; }
        public List<KeyValuePair<string, string>> answers { get; set; }

        string patternNumber = @"\d+$";
        string patternYesNo = @"[^\d]+$";
        public void parse(string filename)
        {
            XDocument doc = XDocument.Load(filename);
            XElement benEvents = doc.Descendants("BENEvents").FirstOrDefault();
            foreach (XElement descision in benEvents.Elements())
            {
                BenEvent newEvent = new BenEvent();
                listEvents.Add(newEvent);
                newEvent.number = int.Parse(Regex.Match(descision.Name.LocalName, patternNumber).Value);
                newEvent.flags = descision.Attributes()
                    .Select(x => new KeyValuePair<int, Boolean>(int.Parse(Regex.Match(x.Name.LocalName, patternNumber).Value), (Boolean)x)).ToList();
                newEvent.question = (string)descision.Element("AskQuestions").Attribute("caption");
                newEvent.answers = descision.Descendants("Question")
                    .Select(x => new KeyValuePair<string, string>(Regex.Match((string)x.Attribute("event"), patternYesNo).Value, (string)x)).ToList();
            }
        }
    }
}

推荐阅读