Welcome back.
In this video, we are going to use link with XML.
And I created a little file here or a little project here called Link with XML.
And I also created this little string here which is called Students XML, and it contains an XML string.
So what this is, is a XML type of object which is called students, which contains multiple objects
inside.
So it contains a student object which has a name of Toni, an age of 21, and a University of Yale for
this example.
And there are three of them.
So XML stands for Extensible Markup Language, which is a language more or less that is used in order
to create data or in order to structure data.
And in this case, we have structured data which is almost like you could see it like a table.
So this is a table called students and it contains multiple students or singular or single students,
and they have a column called Name and A or another column called H and a third column called University.
And it's just structured in a different way, and you will find XML in multiple occasions.
So for example, many websites, they use XML to share their data.
So there are APIs which use XML and you can simply go ahead and use their XML to read the data from
the Internet and then use it in your program.
So let's say you want to show the 100 top movies of the current month.
Then you could go ahead and load that from a website that provides an XML file and you could simply
create your code around this specific structure of the XML file and use it within your program or within
your app or website.
So I have opened up a website here which is Baidu mice dot com.
They have a live soccer XML file, so you have this XML file.
This is of course a lot bigger and has plenty of additional information, but generally it's the same
structure.
So you have this course of the sports soccer.
Then when it was updated, you have the category in which cup it is.
So this was the Albanian cup, for example.
Then you have the matches, the date format, the date, then the match status and so forth.
So this is a lot more complex, but generally it's the same structure.
So you have this matches tag which opens up here and closes right here and you only have one match on
that specific date in that specific category and you had the map status, which is when it started.
And then you also see the goals.
Well, this time in this case, it's a question mark.
So as it seems, this match didn't have any additional information on the goals.
But then there is an Argentinian match here and there are multiple events here.
So there was a goal in minute 19.
Then who scored the goal where it was the local team then who scored the goal as a player?
That was the player El Bima then the the result at that point and so forth.
So you see there are plenty of information which you can go ahead and read from and then show in your
app or in your program.
And this is pretty cool.
So XML is pretty powerful and it's used not for just soccer, it's used for whether it's used for all
kinds of data that should be displayed and you just have to get their API.
So this is just an XML file, which is an example, but otherwise you would have to get their API key
and then you could go ahead and call those XML files and use them in your program.
It's the same for the weather.
So here is a website called Pixel dot com.
And and they have weather information, for example.
So in this case, they have this API for JSON and XML files.
But I'm not going to go into deep into that.
I just wanted to show you some examples.
And generally that's the power of XML and that's why we're checking it out.
So you don't need to access a database in order to use the data in a sufficient way, but you can actually
access XML files or create your own XML files.
But this is the structure that we're going to use.
So we have a student's class or table, if you wish, and in there we have multiple entries.
So we have the student called Toni with the age of 21 and this university is Yale.
All right.
So now let's go ahead and use that.
And in order to do so, we need to add a namespace here.
And the namespace is called System Dot, XML, Dot Link, and now we can go ahead and use this class
because otherwise we cannot use this class which is called X document.
So we create an XML document out of this students.
XML string that we have here.
So I'm going to create a student's x dock file or object here called like that.
And it should be an X document.
All right.
And now we can go ahead and initialize that X document, and we do that the following way.
So X document, dot parse.
So we are parsing from that string, from that student XML string.
Now we can use that in link.
So that's pretty cool.
We can use link not only for objects and only for databases or SQL, which we will see later on, but
we can also use it for XML and that's the power of it.
You know, XML is powerful by itself, but link is even greater because it does all this stuff for us
in the same manner and we just have to give it the data.
And it doesn't matter if it's XML, if it's an object in C sharp or if it's some type of database.
So that's pretty cool.
Now I can create a new variable called students and it will automatically be a collection.
So so far it's just a variable.
It doesn't know what kind of variable it is.
And I'm going to use a link here out of this XML document.
So from student in students x doc dot descendants and and here it's student.
So what is this descendants?
If you hover over it, you see it returns a filtered collection of descendant elements for this document
or elements in document order only elements that have a matching x name are included in the collection
and x name is this student variable here or this argument that we hand over.
So it says, as you can see here, it's an enumerable and.
This student here.
So it's exactly the same typing as you can see here.
So it checks out every single student in our students x doc and the student's x doc takes the student's
x html string, right?
So now it takes every single student descending and it checks them out one by one.
And so it's a just this thing here by itself is a list already.
And we are checking for every single student within that student's list.
So that's what we're doing with this line.
So it's a for each type of line and now we can go ahead and create a new entry.
So select new.
And what do we want to select?
Well, we want to have the name and the name will be the student dot element.
And the element, as you can see here, the student itself here is an X element, as you can see.
So if you hover over it, you see that it's a type X element.
So now we can use this, this method which is called element, and here we have to give a x name.
So an x small name, so to speak, so to speak.
And in this case it's for example, name H University.
So in here the name will be our name.
And I want to have the value so the value property of that specific name, then I want to have the H,
which is student dot element and H and here I also want to have the value.
So now for you as a little challenge, do the same thing with university.
All right.
I hope you tried it.
University student element, that university
value.
And here we may not forget our semicolon.
All right, so that's pretty cool already.
So we have this collection for our students.
And as you can see, it's an innumerable collection which has a string called name, a string called
H and a string called university.
So A is new.
And you see it's anonymous types that we have here.
So now we can go ahead and use that.
So we can use a for each loop, for example, to check out every single student within our students
collection here and print it onto the screen.
So for each and and here it's for student in students.
So in that one.
And what do I want to do?
Well, I simply want to write something onto the console.
So see w double tab and I'm going to say something like student number zero with H of number one from
University of number two.
And who are these numbers?
Well, they are student dot name then student dot H and student dot university.
So that's pretty cool.
So we take this student object, which is within a collection of students.
So every single object of that collection and we can use its properties here.
So as you can see, there is a property called name and now that's pretty cool.
So we can simply check that out console thought right line and now let's run the code.
And there we are, student Tony with the age of 21 from University of Yale.
Then Karla, she's pretty young at Yale with 17 and Leila in Beijing Tech with 19.
So there you are.
You can simply use this type of data, so XML data and use it in the way you would like to or you like
to.
So in our case, we simply used it in order to display it, but you could use it in order to display
it in your app where you have, for example, all the information of a user or of all your students
that you have at uni.
So let's say your professor and you would like to know more about your students and prepare yourself
for the next semester.
Then you could use this XML structure in order to get information about this student and check it out.
So now as a little challenge, that's something that you have seen in the last video, which is sorting.
So please go ahead and sort students by their age.
So the students that we have in our XML file, please go ahead and try that.
All right.
I hope you tried it.
So we use this new variable called sorted students, which is from student in students, and we order
it by student dot age.
And we only select the student itself.
So select student and that's it.
And now we simply use a for each loop again in order to display the students by H.
So I'm going to use not the student, but the store sorted students.
So now I'm going to write the same thing onto the console as we did before, but sorted in by age.
So that's what this new method will do.
And we are student Carla 17, then Layla 19 and 2021.
So you see, before they weren't sorted by age, but now they are.
I hope you try to do that yourself and you manage to do it.
I went over pretty quickly, but that's because we have used the structure and approach already, so
you can simply order it by a specific variable.
So in this case by H.
Okay.
Now the next thing, a little further challenge or additional challenge, please add another type of
information in here and add another student.
So by information I mean something like name, age or university.
It could be the year they are in or the semester they are in.
So please go ahead and try that with this XML file.
So extend it for every single student and then go ahead and create another student.
So just so you get a little bit of a practice with that.
All right.
I hope you tried it.
So I'm going to use semester here.
So we'll be semester and let's say Tony is in the sixth semester and like that we need to close it.
So miss term and now we do the same thing for our other students.
Actually, let's copy the whole line like that.
This way it will be a little better.
So let's say Karla is in the first semester and then we have Leyla and she is in the third semester.
All right.
And now I said, add another student.
So I'm going to copy this student piece here and I'm going to create Frank, who is 25 years old and
he is in his 10th semester.
So she he's studying master's and or even.
Yeah.
Some doctor degrees or something like that.
And yeah, now we can go ahead and extend our code here.
So that's some other challenge that I want to give you.
So please go ahead and extend the code here so that it also shows the semester.
So not just this code here, but actually the for each as well.
All right.
I hope you tried it.
And I'm going to call it semester, which is a student dot element.
And in here it's semester dot value.
And of course, here we need to add the comma.
And let's add the text is in his slush her I'm going to keep it simple because we don't have the gender
in here his her.
And here it's the third or three semester.
And it will not say third or second or stuff like that.
Not going to make it that complicated, but going to keep it simple.
And here it's student dot semester and there we are.
So now we can check it out.
Let's run the code and we should see the semester value of our student as well.
And by the way, if I run the code, I get an error.
And that's really good because as you can see, it's very sensitive here.
So I have semester here and I have semester here and you might have noticed this already.
So it's super important that you have the exact same typing.
So it should be written the exact same way because otherwise you get an error.
It says, Hey, the starting tag and the ending tag, they don't match.
So I'm going to start it again.
And then we are.
So we have student Tony with the age of 21 from university.
Yale is in his her sixth semester.
Then we have Carla who is in her first semester.
We have Layla in her third semester.
And then finally Frank, he's studying forever.
He's in his 10th semester and then they are still ordered here.
And this other for each loop didn't use any additional information, so it didn't know about the semester
because here we didn't use the semester as well.
All right.
So as you can see, this is super extensible, very easy to extend your XML file.
You can just add another tag in here and add information.
You could even leave that open so you don't need to use it in your program.
So let's say we had another type of information.
You don't need to add that.
So if we don't want to use the semester and the age, that's no problem.
So we don't need to use every single tag that is in here in our object, but we can do so and that's
pretty powerful.
All right, great.
So now you have seen how to use link with XML and we are ready to go to school.
So the more extended and more, let's say, powerful way of doing things is to work with a database
yourself.
So let's do that in the next video.
See you there.