May 16, 2016

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

<Rooms>
    <Room RoomNumber="room1" EMAIL="ssds@dsfd.com" dsfdd=""/>
    <Room RoomNumber="room2" EMAIL="ssds@sdd.com" dsfdd=""/>
</Rooms>
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");
http://stackoverflow.com/questions/15267530/how-to-get-the-attribute-value-of-xml-using-linq-to-xml
<appsettings>

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

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

</appsettings> 
XDocument doc = XDocument.Load(@"config_path");

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

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

 doc.Save(@"config_path");

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib