1

I have an XML file from which I am parsing some content to display in a list:

Class:

public class SampleClass 
{
    public string Sample {get; set;}    
    public string Definite {get; set;}
    public string Random {get; set;}
}

XML File Sample:

<Question>
    <Sample>This is sample 1</Sample>
    <Definite>Answer 1</Definite>
</Question>

<Question>
    <Sample>This is sample 2</Sample>
    <Definite>Answer 2</Definite>
</Question>
...

Currently, I am parsing content from the list easily and making this list.

_list = xmlDoc.Descendants("Question")
              .Select(
                  q => new SampleClass 
                  { 
                      Sample = q.Element("Sample").Value, 
                      Definite = q.Element("Definite").Value
                  })
              .ToList();

However, in the list I want to include another element that is to be parsed from the XML file in a random order eg:

SampleClass list   Sample        Definite   Random 
                      ^              ^        ^ 
List element 1: This is sample 1, Answer 1, Answer5
List element 2: This is sample 2, Answer 2, Answer1
List element 3: This is sample 3, Answer 3, Answer4 ...

I wanted to ask how do I include this Random element in the list while parsing such that q.Random is assigned a random <Definite> Value </Definite> from the Question nodes?

Duplicates of random in the list are not acceptable.

user1372448
  • 427
  • 2
  • 12
  • 22

2 Answers2

1

Do it in 2 passes. The first pass can be identical to what you already have. The second pass will assign a random answer to each item in the list.

This is off the top of my head, so forgive any bugs, but it will look something like the following:

IList<string> randomAnswers = _list
    .Select(c => c.Definite)
    .OrderBy(c => Guid.NewGuid())
    .ToList();

for (int index = 0; index < randomAnswers.Length; index++)
{
    _list[index].Random = randomAnswers[index];
}
Seth Flowers
  • 8,990
  • 2
  • 29
  • 42
  • This will produce a list of random answers without repeats. This may or may not be what the OP is after. It means each item is not independently random, since e.g. if there are 5 answers and the first 4 had 2-5 in the random spots, you could guess the last is 1. – Tim S. Jul 02 '12 at 20:17
0

This should be what you're looking for:

var rnd = new Random(); //make this a static field, if needed
var questions = xmlDoc.Descendants("Question").ToList();
_list = _questions.Select(q => new SampleClass
{
    Sample = q.Element("Sample").Value,
    Definite = q.Element("Definite").Value,
    Random = questions[rnd.Next(questions.Count)].Element("Definite").Value
}).ToList();

(from Access random item in list)

Note that this will allow duplicate random answers, e.g. answer 1 could be the Random for 2 and 3, and doesn't prevent an answer from being the Random for itself. If these are problems, you'll need to use a different solution (maybe a variation on this).

Community
  • 1
  • 1
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • Won't this allow duplicate "Random" answers? – Seth Flowers Jul 02 '12 at 20:18
  • Yes, it will. The asker never specified whether he wanted this or not. I'll note that. – Tim S. Jul 02 '12 at 20:19
  • @Tim S. There are repetitions of `Random` element in List elements in this approach. – user1372448 Jul 02 '12 at 20:23
  • So that's a problem? You didn't say so in your question - it'd be good if you edited your question to address the two things I mentioned in my answer: are duplicates acceptable, and are self-references acceptable. Otherwise we're shooting in the dark to try to answer you. – Tim S. Jul 02 '12 at 20:27