1

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

3 Answers 3

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

@Marc's suggestion

    string status = (string)xdoc.Root.Attribute("status");
Sign up to request clarification or add additional context in comments.

1 Comment

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.
5
 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);

Comments

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

is one way.

1 Comment

I think this is not using linq. Is it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.