How can I read the value of an element in an xml file and return the content? [closed]

0

I have an application which has an xml file. This xml file is serialized with the content in which it comes from a variable of a web page.

What I need to do now is get the value of each of the elements in this xml file and compare them in an if.

The goal is that if the username and password are the same as those in the xml file and thus be able to validate the user.

How can I do this?

Thanks

    
asked by A arancibia 19.10.2017 в 18:15
source

1 answer

0

I give you an example:

public static Usuario ObtenerUsuario(string nombreUsuario, string clave)
    {
        XElement root = XElement.Load(HttpContext.Current.Server.MapPath(@"~\App_Data\usuarios.xml"));
        IEnumerable<XElement> results = from m in root.Elements("Usuario")
                                        where (String)m.Attribute("usuario") == nombreUsuario
                                        select m;

        Usuario objUsuario = null;

        if (results.Count() > 0)
        {
            objUsuario = new Usuario();
            XElement usuario = results.ToList()[0];
            XmlSerializer serializer = new XmlSerializer(typeof(Usuario));
            objUsuario = (Usuario)serializer.Deserialize(usuario.CreateReader());
        }

        return objUsuario;
    }

And your User object would be:

public class Usuario
{
    [XmlAttribute]
    public String usuario { get; set; }
    [XmlElement]
    public String clave { get; set; }
}

Greetings.

    
answered by 19.10.2017 / 18:57
source