0

So I'm working with reading an xml file to create a dictionary, but I can't figure out how to access the xml fields I want.

Below is the format of the XML I want to read.

<Days>
    <Day Name="Monday">
        <Task Order="1">TestTask</Task>
        <Task Order="2">Test2</Task>
    </Day>
</Days>

Below is my code so far. I've tried a lot of variations for finding task and order, such as for task: (string)e, or e.ToString(), or e.Elements("Task").Value.ToString(); And for order e.Attributes("Order").ToString();

        string today = DateTime.Now.ToString("dddd");           
        var allItems = new Dictionary<string, int>();

        XElement root = XElement.Parse(_orderxml);
        IEnumerable<XElement> address =
            from el in root.Elements("Day")
            where el.Attribute("Name").Value == today
            select el;
        foreach (XElement e in address)
        {
            string task = ???;
            string order = ???;
            allItems.Add(task, (int)order);
        }

So far, none of these have given me the right results, and I'm really unsure of what the proper way to get this data is, so any help would be appreciated!

1 Answer 1

2

Add a second loop to iterate the tasks and extract the values

static void Main()
    {
        string _orderxml = @"<Days>    <Day Name=""Wednesday"">        <Task Order=""1"">TestTask</Task>        <Task Order=""2"">Test2</Task>    </Day></Days>";
        string today = DateTime.Now.ToString("dddd");
        var allItems = new Dictionary<string, int>();

        XElement root = XElement.Parse(_orderxml);
        IEnumerable<XElement> address =
            from el in root.Elements("Day")
            where el.Attribute("Name").Value == today
            select el;
        foreach (XElement e in address)
        {
            foreach (XElement t in e.Descendants())
            {
                string task = t.Value.ToString();

                int order = int.Parse(t.Attribute("Order").Value.ToString());
                allItems.Add(task, (int)order);
            }
        }
    }

Or you can do it with a Linq query like this

var result=root.Descendants("Day").Where(d=>d.Attribute("Name").Value==today).Descendants("Task").Select(x => new {Task=x.Value,Order=x.Attribute("Order") });

Or create a dictionary from the anonymous objects

var result = root.Descendants("Day").Where(d=>d.Attribute("Name").Value==today).Select(x => new { Task = x.Value.ToString(), Order = x.Attribute("Order") }).ToDictionary(c => c.Task, c => c.Order);

Or create a dictionary directly from the linq query

var result = root.Descendants("Day").Where(d=>d.Attribute("Name").Value==today).ToDictionary(c => c.Value.ToString(), c => int.Parse(c.Attribute("Order").Value.ToString()));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.