简体   繁体   中英

get xml attribute using linq

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

    XDocument xdoc = XDocument.Load("file name");
    // string status = xdoc.Root.Attribute("status").Value;

@Marc's suggestion

    string status = (string)xdoc.Root.Attribute("status");
 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);
XmlDocument doc = new XmlDocument();
doc.Load(...);
doc.DocumentElement.Attributes["status"].Value

is one way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM