开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜