简体   繁体   中英

Parsing XML metadata containing conditional statements with C# in Unity

I have an XML file full of events like this one with multiple statements to check.

How is this action even called? I'm so lost that I don't even know where to start looking.

<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>

Please help to point me in the right direction.

EDIT: THe answer solved my question, thatnk you

This is how my code ended up for now to deal with more general events.

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;
    }

}

This is not a simple Xml Parse. See my Xml Linq solution using Regex

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();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM