XML RECORD VALUE IN MVC

Pooja Malohtra

Total Post:47

Points:331
Posted by  Pooja Malohtra
C#  XML  Mvc4  XML Reader 
 924  View(s)
Ratings:
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!

  1. Maria Susan

    Post:29

    Points:107
    Re: XML Record Value in mvc

    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");

        }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!