May 16, 2016

How to get the attribute value of xml using LINQ to XML

    <Room RoomNumber="room1" EMAIL="" dsfdd=""/>
    <Room RoomNumber="room2" EMAIL="" dsfdd=""/>
var doc = XDocument.Load(HttpContext.Current.Server.MapPath("~/config.xml"));
// or doc = XDocument.Parse(myXmlString);

string roomNumber = "room1";

var emailQuery = from room in doc.Root.Elements("Room")
                 where (string)room.Attribute("RoomNumber") == roomNumber
                 select (string)room.Attribute("EMAIL");

    <add key="MDXQueryDirectory" value="" />

    <add key="URLPrefix" value="/Manager" />

XDocument doc = XDocument.Load(@"config_path");

 var element = doc.Descendants().Elements("add")
               .SingleOrDefault(x => x.Attribute("key").Value == "MDXQueryDirectory");

 element.SetAttributeValue("value", txtMDXQueryDirectory.Text);



Post a Comment

Nam Le © 2014 - Designed by, Distributed By Templatelib