tags:

views:

126

answers:

3

suppose i have xml similar to the below

<?xml version=”1.0” encoding=”UTF-8”?> 
<validate status=”yes” last_updated=”2009-07-05T11:31:12”> 
 etc...etc
</validate>

in c# how can i get the value of status in the validate element?

there will only be one validate element. how can i do this with linq?...or if theres a simpler way maybe

A: 
XmlDocument doc = new XmlDocument();
doc.Load(...);
doc.DocumentElement.Attributes["status"].Value

is one way.

John Gietzen
+4  A: 
    XDocument xdoc = XDocument.Load("file name");
    string status = xdoc.Root.Attribute("status").Value;
Mehdi Golchin
Minor suggestion: use: `string status = (string)xdoc.Root.Attribute("status");` - then you get `null` if the attribute doesn't exist (easy to test for), rather than an exception.
Marc Gravell
+1  A: 
 string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?> 
<validate status=""yes"" last_updated=""2009-07-05T11:31:12""> 
 etc...etc
</validate>
";

            var doc = XDocument.Parse(xml);
            var item = doc.Elements("validate").First().Attributes("status").First().Value;

            Console.WriteLine(item);
BFree