2

I am trying to get sorting working for certain columns from an xml file. Here is what I have.

    public ActionResult Index(string sortOrder)
    {
        IEnumerable<Personal> personals;
        XmlDocument doc = new XmlDocument();
        List<Personal> persons = new List<Personal>();
        doc.Load("C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml");
        foreach (XmlNode node in doc.SelectNodes("/Persons/record"))
        {
            persons.Add(new Personal
            {
                ID = node["ID"].InnerText,
                Name = node["Name"].InnerText,
                Email = node["Email"].InnerText,
                DateOfBirth = node["DateOfBirth"].InnerText,
                Gender = node["Gender"].InnerText,
                City = node["City"].InnerText
            });
        }
        switch (sortOrder)
        {
            case "ID":
                personals = persons.OrderBy(Personal => Personal.ID);
                break;
            case "Name":
                personals = persons.OrderBy(Personal => Personal.Name);
                break;
            case "City":
                personals = persons.OrderBy(Personal => Personal.City);
                break;
            default:
                break;
        }
        return View(persons);
    }

Things I've tried include .getElementByTagName("ID").toString().OrderBy(...), to no avail. I'm pretty sure I've got syntax all wrong. Please help and thank you.

1
  • personals != persons Commented Sep 26, 2018 at 21:51

2 Answers 2

4

I think you need to let the was sorted collection result set to persons otherwise, the collection wasn't ordered by.

switch (sortOrder)
{
    case "ID":
        persons = persons.OrderBy(Personal => Personal.ID).ToList();
        break;
    case "Name":
        persons = persons.OrderBy(Personal => Personal.Name).ToList();
        break;
    case "City":
        persons = persons.OrderBy(Personal => Personal.City).ToList();
        break;
    default:
        break;
}
return View(persons);

I would use linq instead of foreach let the code more clear.

SelectNodes("/Persons/record").Cast<XmlNode>() use Cast<XmlNode>() let XmlNodeList to IEnumerable<XmlNode> collection then use linq select.

public ActionResult Index(string sortOrder)
{
    XmlDocument doc = new XmlDocument();
    doc.Load("C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml");
    IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record")
        .Cast<XmlNode>()
        .Select(node => new Personal()
        {
            ID = node["ID"].InnerText,
            Name = node["Name"].InnerText,
            Email = node["Email"].InnerText,
            DateOfBirth = node["DateOfBirth"].InnerText,
            Gender = node["Gender"].InnerText,
            City = node["City"].InnerText
        });
    switch (sortOrder)
    {
        case "ID":
            persons = persons.OrderBy(Personal => Personal.ID);
            break;
        case "Name":
            persons = persons.OrderBy(Personal => Personal.Name);
            break;
        case "City":
            persons = persons.OrderBy(Personal => Personal.City);
            break;
        default:
            break;
    }
    return View(persons.ToList());
}
Sign up to request clarification or add additional context in comments.

6 Comments

That just gave me another error, taking it out of context(List)
@MathewDodson you just add .ToList(), because I think your view need to use List<Personal> type
Now it's giving me 'use of unassigned local variable, personals'. Trying everything.
@MathewDodson I edit my answer and add another solution I think is better way.
Or just return View(personals)
|
0

Firstly you need return personals

...
return View(personals);

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.