-2

Hello I have created an xml file with motivational quotations, and I want to read those quotations into an array.

Here is what my xml file looks like:

<?xml version="1.0" encoding="utf-8" ?> 
<MotivationalQuotes>
  <quotation>
    <quote>Life is about making an impact, not making an income</quote>
    <author>Kevin Kruse</author>
  </quotation>

  <quotation>
    <quote>Whatever the mind of man can conceive and believe, it can achieve</quote>
    <author>Napoleon Hill</author>

  </quotation>

</MotivationalQuotes>

I am trying to store each individual quotation (without the author) into an array, so far I have the below code working - which creates a message box and iterates through the xml file displaying the text from each quotation.

1) How can I modify this code to create a string array, where each item in the array is a quotation (i.e. each item in the array is the contents that is currently being displayed to the messagebox in my foreach loop?

2) how do I return a random item from the array, once it is created?

3) As an extension to my question... my xml file only has motivational quotes in at the moment, but it will have more inspirational, funny etc... how can I specify to only include quotations into the array if they are inside the MotivationalQuotes tag.

Thanks for the help!

public void motivate()
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("quotations.xml");
                XmlNode Node = doc.DocumentElement;

                foreach (XmlNode Node1 in Node.ChildNodes)
                {
                MessageBox.Show(Node1.FirstChild.InnerText);

                } 
            }
GaryDev
  • 15
  • 8
  • Perhaps if you could motivate yourself to read the rules first.... 1 question per question. Questions must be specific. You must have tried it yourself first. And so on. You need to read them for yourself. – JK. Sep 21 '15 at 00:05
  • Welcome to StackOverflow! While we definitely want to help people with their coding issues here, and especially welcome questions from new users, the format of questions is very important. This question appears to be very broad, asking multiple questions at once, which makes providing useful answers difficult. Please read a bit more of the site rules at [ask]. – Claies Sep 21 '15 at 00:08
  • 1
    And you will certainly not find people more willing to answer your questions if you are dismissive of the site rules. – Claies Sep 21 '15 at 00:09
  • Thanks for the advice Claies, I will post again as single questions. – GaryDev Sep 21 '15 at 00:10
  • by the way this is a very antisocial site from what i can see. – GaryDev Sep 21 '15 at 00:12
  • 1
    On the contrary; the vast majority of people are very helpful and engaging. However, it is, by it's nature, a large group of professional programmers who love a challenge, but expect the people they help to demonstrate an equal measure of effort. – Claies Sep 21 '15 at 00:14

1 Answers1

0

You should use XDocument and LINQ.

To get all quotes

using System.Xml.Linq;

var quotes = XDocument
 .Load("quotations.xml")
 .Descendants("quote")
 .Select(q => q.Value)
 .ToArray();
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Thanks for your help Richard, I will try this. – GaryDev Sep 21 '15 at 00:24
  • Someone unhappy you helped me i imagine, my post was already downvoted twice before i even had a chance to read the barbed comments. Some very petty people. I truly appreciate your response, thank you! – GaryDev Sep 21 '15 at 00:25
  • Thanks I tried to upvote but it wouldn't let me it told me i need 15 reputation, i will accept as answer i am just trying to get it to work, right now my xdocument does not contain a definition for descendants, i have added using System.Xml.Linq; to my file but think i have to add something else, so will figure it out and then mark as answered when i get it working, thankyou! – GaryDev Sep 21 '15 at 00:31
  • Oops a typo, use `Descendants` – Richard Schneider Sep 21 '15 at 00:35