Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Group>
- <Entry>
- <Name> Bob </Name>
- <ID> 1 </ID>
- </Entry>
- <Entry>
- <Name> Larry </Name>
- </Entry>
- </Group>
- var items = from g in xDocument.Root.Descendants("Group").Elements("Entry")
- select new
- {
- name = (string)g.element("Name").Value,
- id = g.Elements("ID").Count() > 0 ? (string)g.Element("ID").Value : "none"
- };
- var items =
- from g in xDocument.Root.Descendants("Group").Elements("Entry")
- select new
- {
- name = (string) g.Element("Name"),
- id = (string) g.Element("ID") ?? "none",
- };
- var items =
- from g in xDocument.Root.Descendants("Group").Elements("Entry")
- select new
- {
- name = (string) g.Element("Name"),
- id = (int?) g.Element("ID"),
- };
- public static string OptionalElement(this XElement actionElement, string elementName)
- {
- var element = actionElement.Element(elementName);
- return (element != null) ? element.Value : null;
- }
- id = g.OptionalElement("ID") ?? "none"
- var items = from g in xDocument.Root.Descendants("Group").Elements("Entry")
- let idEl = g.Element("ID")
- select new
- {
- name = (string)g.element("Name").Value,
- id = idEl == null ? "none" : idEl.Value;
- };
Add Comment
Please, Sign In to add comment