Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <foo>
- <bar baz="This is the value of the attribute named baz">
- </foo>
- XPathDocument document = new XPathDocument(filename);
- XPathNavigator navigator = document.CreateNavigator();
- XPathNavigator node = navigator.SelectSingleNode("//foo/bar");
- if(node.HasAttributes)
- {
- Console.WriteLine(node.GetAttribute("baz", string.Empty));
- }
- XDocument doc = XDocument.Load("foo.xml");
- var att = from a in doc.Descendants("bar")
- select a.Attribute("baz")
- foreach (var item in att) {
- if (item != null) { ... }
- }
- var node = XDocument.Load(filename)
- .Descendants("bar")
- .SingleOrDefault(e=>e.Attribute("baz") != null);
- if (node != null) Console.WriteLine(node.Attribute("baz").Value);
- var baz = navigator.SelectSingleNode("//foo/bar/@baz");
- if (baz != null) Console.WriteLine(baz);
- var doc = XDocument.Load(fileName);
- var results = from r in doc.XPathSelectElements("/foo/bar[count(@baz) > 0]")
- select r.Attribute("baz");
- foreach (String s in results)
- Console.WriteLine(s);
Add Comment
Please, Sign In to add comment