DRAG DROP
You are developing a C# application. The application includes a class named Rate. The following code segment implements the Rate class:
You define a collection of rates named rateCollection by using the following code segment: Collection
The application receives an XML file that contains rate information in the following format:
You need to parse the XML file and populate the rateCollection collection with Rate objects.
How should you complete the relevant code? (To answer, drag the appropriate code segments to the correct locations in the answer area. Each code segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.)
Select and Place:
is also an element not value of the element
Here is a catch ” is also an element not value of the element”. Using MoveToElement() method reader meves back to the element, but doesn’t have value. You must use ReadToFollowing(“value”) in order to read ‘s value.
Sorry, to correct myself. I have tried it out and reader.ReadToFollowing(“value”) is definitely correct. If you want to try yourself, here is the code
private static void ReadXml()
{
var rates = new List();
using (XmlReader reader = XmlReader.Create(
new StringReader(
” ” + “”
+ ” ”
+ ” 0.0375″ + ” ”
+ ” ”
+ ” 0.0475″ + ” ”
+ “”)))
{
while (reader.ReadToFollowing(“rate”))
{
var rate = new Rate();
reader.MoveToFirstAttribute();
rate.Category = reader.Value;
reader.MoveToNextAttribute();
rate.Date = DateTime.ParseExact(reader.Value, “yyyy-mm-dd”, CultureInfo.InvariantCulture);
reader.ReadToFollowing(“value”);
rate.Value = decimal.Parse(reader.ReadElementContentAsString(), CultureInfo.InvariantCulture);
rates.Add(rate);
}
}
rates.ForEach(rate => Console.WriteLine($”{rate.Category} {rate.Date:d} {rate.Value}”));
}
public class Rate
{
public string Category { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
}
I think this is wrong and Q27 is correct: MoveToElement() moves the reader from the attribute back to the element node. There you can read the value of the element.
See example in https://msdn.microsoft.com/en-us/library/system.xml.xmlreader.movetoelement(v=vs.110).aspx
This is the correct solution. The default answer to Q27 is wrong
Question number 27 has reader.MoveToElement() for Target 4. Where is the truth?