Home > DeveloperSection > Forums > XML Record Value in mvc
Pooja Malohtra
Pooja Malohtra

Total Post:47

Points:331
Posted on    November-09-2014 10:43 PM

 ASP.NET MVC C#  XML  Mvc4  XML Reader 
Ratings:


 1 Reply(s)
 610  View(s)
Rate this:

I have an xml file:

<profiles>
  <profile username="user4" fullname="Full Name" />
 </profiles>

I am trying to retrive the value of the fullname, here is what I tried:

public List<string> GetFullName(string username)
{
    List<Profile> list = null;
    try
    {
        list = (from t in ProfileList
                where t.UserName == username
                select t).FirstOrDefault();
    }
    catch { }
    List<string> userFullName = new List<string>();
    if (list != null)
    {
        foreach (Profile t in list)
        {
            userFullName.Find(t.FullName);
        }
    }
    return userFullName;
}

FirstOrDefault gives an error!



Maria Susan

Total Post:29

Points:107
Posted on    November-10-2014 12:53 AM

I'd rather rewrite your code like this

    public String GetFullName(string username)

    {

        var targetObject = (from t in ProfileList

                    where t.UserName == username

                    select t).FirstOrDefault();

 

        if (targetObject != null) return targetObject.FullName;

        throw new Exception("Target user is not found");

    }


Don't want to miss updates? Please click the below button!

Follow MindStick