From the course: .NET Essentials: LINQ for XML

Quick look at the XElement class - .NET Tutorial

From the course: .NET Essentials: LINQ for XML

Start my 1-month free trial

Quick look at the XElement class

- Before we look at other ways to load data, let's examine the XElement class. I'm working in this file 4.1, working with the XElement, and I'm working with two files. There's the BigStarCollectibles. Remember that's the one that has the XML namespace with the schema. And then this one called Noschema. That's the file that doesn't have any schemas. And I'm loading both of those into an XElement. And then on the bottom half of the screen, I'm outputting some information. So I'll run the coding and we'll start by taking a look at what we're doing here on line 10. I'm getting the full name of the element which is you can see is here, System.Xml.linq.XElement. And then the next thing I do, is I dump the name of the element. Now let's review what's in this file. This is the file there's collectibles, and there's the XML namespace and beneath collectibles or the individual card elements. There's eight of those. So what I'm getting back here when I call Name, is I'm getting the name of that element so I can see the name of it is Collectibles but you notice that is prefixed with the schema name. You can look at the scheme over here. It's bigstarcollectibles.com/course-examples. And I see that here, it's in these curly braces. I also see it when I dumped out the Namespace here I see that without the curly braces. Now, when I dump out the file with no XML schema, you notice that it doesn't prefix it with the namespace, here it just shows collectibles. And when I attempt to get the Namespace for that file, I get back an empty string. To repeat what we've seen, when we load the data, we're getting all the XML, which is a hierarchical tree that starts with the collectibles element and contains all the nested children elements. Again, go over here and look I'm getting Collectibles and all of the nested children. And when there's an XML Namespace in the root element you will need to fully qualify names to work with the data.

Contents