-1

I have the following XML structure for a card game. I want to load the card titels and descriptions into two arrays that I can use to randomize the cards.

    <Cards>
      <CardTitles>
        <Title>Some Title</Title>
                   .
                   .
                   .
                   .
                   .
     </CardTitles>
     <CardDesc>
       <Desc>Some description</Desc>
     </CardDesc>
    </Cards>

But no matter what I do or what code I write I'm unable to get the actual text from the proper tag. The closest I got was following this example :https://msdn.microsoft.com/en-us/library/system.xml.xmlreader.readsubtree(v=vs.110).aspx

I know I'm not supposed to ask for complete solutions but I'm just stumped. Any help in getting this matter cleared up to me will be great.

Uri Popov
  • 2,127
  • 2
  • 25
  • 43
  • 1
    What exactly are you having issues with? There must be literally hundreds of examples and questions on this topic – Patrick Mar 21 '16 at 15:30
  • @Patrick . There are but I just cant wrap my hear around them. All I want is to get the inner text of all Title tags for example into a string array. – Uri Popov Mar 21 '16 at 15:32
  • Well if that is the *exact* xml structure you will probably get an error. You have an unclosed Title tag and dots in the middle of it. That's not valid... – Patrick Mar 21 '16 at 15:48
  • @Patrick the dots are just to illustrate that there are more title tags than just he one X_X . – Uri Popov Mar 21 '16 at 16:06

2 Answers2

1

Supposing that you have an xml file named sample.xml in C:\temp you can use LINQ To XML:

   XElement x = XElement.Load (@"c:\temp\Sample.xml");

   IEnumerable<string> titles = from title in x.Element("CardTitles").Elements()
                                select title.Value;
   IEnumerable<string> descriptions = from description in x.Element("CardDesc").Elements()
                                      select description.Value;
V. Oikonomakos
  • 119
  • 1
  • 5
  • ok. this seems like a really straight forward solution, Question is will this work with a unity3d project and will it run on both iOS and android ? – Uri Popov Mar 21 '16 at 16:57
-1

Instead of going XmlReader route, you can use XmlSerializer which is much more simple and straightforward to use.

https://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx

You'd have something like this:

<Cards>
      <CardTitles>
        <Title>Some Title</Title>
     </CardTitles>
     <CardDesc>
       <Desc>Some description</Desc>
     </CardDesc>
</Cards>

.Net Classes

public class Cards {
    public CardTitles CardTitles;
    public CardDesc CardDesc;
}

public class CardTitles {
    public String Title;
} 

public class CardDesc {
    public String Desc;
}

And then use XmlSerializer.Deserialize method.

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Cards));
StringReader inputStrReader = new StringReader(inputString);
Cards cards = (Cards)xmlSerializer.Deserialize(inputStrReader);
TchiYuan
  • 4,258
  • 5
  • 28
  • 35