Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

extracting data from an xml file using c#

Discussion in 'Getting Started' started by gamerbrain84, Aug 26, 2015.

  1. gamerbrain84

    gamerbrain84

    Joined:
    Aug 16, 2015
    Posts:
    1
    Hey, new to Unity and utterly stumped by this problem... traipsed round every bloody page available and everything seems to not work. Help would be incredible...

    I have an xml file that looks like this:

    1. <eventList>
    2. <event>
    3. <number> 0 </number>
    4. <Title> Research Discovered </Title>
    5. <BodyText> You have found research to advance the cause.</BodyText>
    6. <ButtonText> Continue </ButtonText>
    7. </event>
    8. <event>
    9. <number> 1 </number>
    10. <Title> Research Discovered </Title>
    11. <BodyText> You have found research to advance the cause.</BodyText>
    12. <ButtonText> Continue </ButtonText>
    13. </event>
    14. <event> 0
    15. <number> 2 </number>
    16. <Title> Research Discovered </Title>
    17. <BodyText> You have found research to advance the cause.</BodyText>
    18. <ButtonText> Continue </ButtonText>
    19. </event>
    20. </eventList>
    I want to be able to
    1. load in the xml file (there seem to be a million ways to do that, not sure of the best one for the purpose
    2. Read through it and find the 'event' that matches a random number I pass in (I know how to create the random number... that bit's fine).
    3. Return the 3 strings 'Title', 'BodyText' and 'Continue'.
    Effectively it will be a random event's generator, giving back the data that I can then pass to the UI.

    I'm not expecting someone to write the code for me here, but I would appreciate if anyone knows how they came to learn how to do this and passing on their source. Is there a good video or tutorial that I simply can't find? What should I be looking for? There's xmlReader, xmlDocument, Linq, xDocument, XmlNodeReader... the list goes on... all of them seem to run into the following roadblocks at some point:

    • can't load the file in
    • illegal characters in path
    • data type not recognized
    Cos there are so many ways of doing this I simply don't know which root to persevere with.

    Thanks to anyone that can help - this problem is near breaking me...
     
  2. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
  3. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    Your questions 1 and 2 really get at the crux of the matter; 3 should be clear once you understand 2.

    Question 1, how load the file, depends on exactly how you intend to use this. For example, if you want to have the xml as a local file that you package along with the game, the best approach would be to put the file in the project somewhere and then reference it as a TextAsset. Alternatively Gnatty's link suggests loading it from Application.dataPath, an approach that also works fine, although it can get a little dicey making sure the file is correctly included in the final deployed app.

    If on the other hand you want to load an xml file from the Internet, then you first need to learn how to use coroutines to download data from the Internet. Basically, use the WWW class in the coroutine, and the 'text' property will be a string with the xml in it.

    Question 2 is mostly about parsing the data once you have the file loaded. Again the XmlSerializer approach discussed in Gnatty's link would work fine, but that does require writing up a separate class to put the data into (the jargon term is "deserialize into"). I usually prefer to load the data into XmlDocument (look up the method LoadXml) and then search within the nodes of that object.

    (incidentally, loading and parsing xml data from the Internet is discussed in chapter 10 of my book Unity in Action).
     
    gamerbrain84 and Ryiah like this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    C# does have a confusing plethora of ways to deal with XML. I'm working on a project now that does extensive XML parsing, and after trying a couple of different approaches, I settled on using "LINQ to XML", which is .NET's newer, gentler approach to the problem.

    I don't actually use the "LINQ" (language-integrated query) part of this, but I do use things like .Descendants to find all nodes under this one of a particular type. (Or when you only want to look at immediate children, you use .Element or .Elements instead).

    Here are the key steps to get started:
    1. At the top of your file, add these "using" statements:
      Code (CSharp):
      1. using System.Collections.Generic;
      2. using System.Xml;
      3. using System.Linq;
      4. using System.Xml.Linq;
    2. Load your document with a line such as:
      Code (csharp):
      1. XDocument xdoc = XDocument.Load(filePath);
      Note that if this step fails, you don't have an XML problem, you have a path problem — fix your path!
    3. Now use .Descendants, .Element, etc. on this xdoc (or any child node you get from it). Remember to use .Value to get the actual value of an element.
    It's pretty easy from that point. If you see any docs or StackOverflow questions or whatever about "XPath," run the other way — that is the older, less friendly way of dealing with XML, and it will only confuse you. Stick to the types XDocument, XElement, XAttribute, etc., and you should be good!
     
    Ryiah likes this.