XElement root = XElement.Load(xmlReader);
IEnumerable<XElment> items = root.Elements("?????????");
Where the ???? is, can I add a path or it has to be a single xml element name?
ie. can I do /blah/blah2/asdf ?
XElement root = XElement.Load(xmlReader);
IEnumerable<XElment> items = root.Elements("?????????");
Where the ???? is, can I add a path or it has to be a single xml element name?
ie. can I do /blah/blah2/asdf ?
If you try to pass an XPath you will generate this exception:
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: The '/' character, hexadecimal value 0x2F, cannot be included in a name.
You must pass the element name as a string. That string is implicity converted to an XName
type that has restrictions over what characters can and cannot be in the string (/
,<
, >
, etc.).
No, the parameter to the Elements extension method needs to be a single element name (an XName actually, but if you don't need namespaces, just pass the element name as a string), not an XPath.
If you want to select XElements using XPath, there is also an extension method for that. Include System.Xml.XPath and do:
IEnumerable<XElement> items = root.XPathSelectElements("your/xpath");
???? should be a single xml element name.
Strictly saying, The Elements()
methods accepts an XName
argument. Fortunately, there is an implicit conversion from string
to XName
.
XName name = "Book";
XName name2 = "{http://schemas.company.com/books}Book"; // XName with a namespace
To select a path, use the extension methods from the System.Xml.XPath
namespace (System.Xml.Linq
assembly):
IEnumerable<XElment> items = root.XPathSelectElements("Books/Author");