LINQ in C#.Net made easy! - PART 4 : LINQ to XML | Create XML Files using LINQ | Codelligent

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to another video of code Allegiant as you are aware that we are progressing in our link your series so as part of that we will today we will look into link you to XML so let us try to understand what exactly is link of XML link you to XML as per Microsoft is a link you enabled in-memory XML programming interface that enables you to work with XML from within the dotnet framework so that is they are providing a set of tools using which you can work with XML files you can read XML files you can search in the XML nodes you can create new XML nodes and you can write into files so all these link you to XML related functionalities you will get it by using the namespace system dot XML dot linked you so most of the times you will be working with classes which are provided in this namespace that is X element and X attribute so let's see how we can do it so when you have to load a new file if you are provided with a XML file which is already existing then you can use X element dot load and you can specify the path of the file so automatically this will resolve that XML it will parse that XML and the complete data will be returned and as you are already aware that link you can loop through the I n numeral table types or collection types so this data which it comes after parsing the XML file again you can use link you on that so you can the link you provides something called descendants elements attribute etcetera so that we will be seeing in detailed in the code so you can use them saying that data dot descendants and search for a node name in this particular complete XML hierarchy and you if you want to filter out you can use the where condition or if you want to order it you can use order by Clause so all those classes which you have seen till now you link you is applicable here also so if you want to create a new XML element then you can directly create new X element and give the name and inside that you can say a comma you create more excitement so it will be always creating a new x element so the way you define it this level at which you define that defines the structure of your XML so for example here first x element is the name person's comma you are providing the inside that so whenever you are saying comma that is like next element will be inside that so that will be X element person so comma inside that you will provide new X element that is name with value thermos comma and now you can see it is not inside this X element which we have put this comma it is at this level so there that's why you will see two of these nodes name and age inside the person element and this one n will be inside the person's element so this is how you define a new x element using the X element class so we will try to see this and use it in our code and we will try to understand it much better so now I am in visual studio and this is a simple console application which I have created and for the ease of the work I have already created XML file which is having some students information so for example the root node says students inside which you have student elements so this is these are different students which we have in each student there are elements like name class and marks inside marks we have multiple subject and each subject has an attribute called title and a value which is that particular subjects marks so this is the XML which we'll be working on so for the ease of reference I will just put it in two sides so that we can always view the data here yeah so now if this data is this XML is stored at us place called D XML data and the external file name so this is the file name path which I this is saved so I have just taken that into a string variable so now I want to load this XML into memory so what I can do is can say we're students data is equal to you remember the X element class so we can use X element so now you can see this comes from system dot XML dot link you so X element dot it provides a method called load so if you see in this the load accepts data from a stream it accepts a URL of the file it accepts a XML reader or you can pass even text reader so these different type of inputs it accepts so wherever your XML data is if whether it is in-stream or whether it is in a file all those things you can pass and this particular method will parse it and give you the output so here in this case we have a path which is of string type so that is nothing but about file path so I will just pass this and just end this statement so now this load will read all this data and gives in given this particular variable which is of type X element so if you want to check I can just put a debug point here and I can run this so once it is run you can just see this variable so this is the data which is read into that so this is the whole XML which we have here which has been parsed and taken into the memory so now once this data is available now you can do lot of link to operations on this and you can filter out the data order the data group the data whatever concepts we have learnt till now in link you you can apply on this so for example maybe we will look into one scenario wherein suppose out of this all the student data we want to filter out the data of those students who are in class end so this particular student with named Thomas is in class tenth and you will see there is another student called raj who is in class tenth so we our aim is to loop through this data and fetch the student information who are in class tenth so let's see how we can do it so I'll say where data some variable which I have declared now I will use the student data variable which I have already fetched so I'll say students data dot yeah so I'm will be using the method based syntax here so the same can be used using your query base syntax so whichever you are comfortable with you can use it so I feel comfortable in this scenario to use the method base so I am going ahead with in the third base so once you have the data you are to parse in that there are different ways of achieving it like Excel elements descendants and element so if at all there are multiple elements for example student elements are repeating so this is a set of data which you want you are repeating so it's always good to use descendants so when you say descendants so descendants will fill will fetch you an ienumerable type that is a list of data type so in this you can filter out so what do you want to filter out you can provide the name so the name filtering I want to do is based on student so I am saying student so now it will fetch all the student element so that means it will fetch this it will fetch this it will fetch this and it will fetch this so we will get these data now since it is again a list on top of that we can apply lot of link you operations so I want to filter out so for filtering if you remember there is a where method which we exist so I will say dot where so now in this where I have to give the condition if you remember how we write the conditions in the where clause and other classes in method by syntax it is using lambda expressions so I will say SP some variable so to represent every student data which we are having lambda operator so now here I can say student that is each student so now it will return me every students data in that data what I want to fetch so I want to fetch the element named class so I will say element and I can say which element I want I want class element so now I now when I loop through this and I get to say dot element of class it will give me this particular data and it will loop every student information and it will keep on fetching this particular data so now what I want to see here is this class data that is the value is equal to ten so one property which X element provides so if you see this element provides type X element so if you go to in the X element information so that is X element class here if you scroll down you can see there are lot of operator overridden here so that is nothing but it fail you have a direct explicit conversion for boon for string for decimal for int for long float date-time timespan etcetera so all these type of data these all data types can be directly type casted so you need not do anything you can directly convert this into int when you know that the class data is will be of type end now I can compare it with whatever value I want else if you don't want to do this then there is one more option like you can fetch the value there is a value property in each element so when you say that element dot value you will get this but again this value will be always in type of string so if you want you can do again into dot parts of this string and get the integer output of that instead you can directly use this typecast which this link your XML provides you so now when you say this now you are done with your filtering so now in this data you will have only the records of those students which have the class elements value is equal to 10 you are to verify this just press f5 and let see the data so now when you say data it is having a result view and when you expand result you see two information that is Thomas and Raj so if you see here Thomas and Raj they are the two who are in class 10 so now you have an ienumerable of excel so now let's try to print the data so for which I will use a 4h and maybe a term or I will say instead of item I'll say student and in data so now I have a looping in through all these students so now what I want to print first I want to print the name and then maybe I want to print the subject and marks so how will I do that so first name is at the in student level itself so I can directly say console dot write line and here I will say or maybe instead of name I can directly print the name so that is like the student dot now this student gives you this particular one information so I want to fetch the name element under that so I can say dot element of inside the element I can give the name that is named element so I want to fetch this particular information so when I do that it will give me the name now next you want to fetch this information so now it is again a kind of collection so for that what you have to do first is where I will say marks so now I have to fetch us list of data so again what I have to do is I will say student dot descendants so now descendants will give me the list of lists of elements so which descendants I want I want subject so now here we were fetching one element that is like only one class whereas here we want multiple so that's why we are using descendants so now in this I can do a for each again so here it will be maybe I'll write item itself and here it is marks so this variable names you can give whatever you want so I will again here console dot write line and some space I will leave so that I can get it under the name so that is printed in that form so first I will print the subject name so again here if you notice you will be getting each item that means it you will be getting one one subject so in this subject what are things we want to print first is you want to print the attribute that is the title and then the value so how you will fetch the attribute so now you have this element subject element so now you what you'll do is item dot you want to fetch the attribute under that element so you can say attribute which attributes so you should provide the name of the attribute the name of the attribute is nothing but title and I'll put a colon and then I will again say item dot value so this value this particular item ID how I am having so that items value I am taking and I am printing so when you run this so now you can see it is getting printed here yes so now but one difference you will see here it is title equal to math so that means it is fetching the whole attribute itself so it is trying to print the whole attribute so if you don't want that then you can just say print only my value so in that case it will print only the value of that attribute so now you can see that is the data here also the same thing is happening I think it is fetching the whole name element so instead of that we can say dot value so now we will get the current data so this is how you work with looping through this data so here instead of where this simple condition you can put more complex conditions and all so maybe like we can see one more scenario where I will change this data now like maybe I can remove this as of now so now imagine another scenario wherein you want to fetch the information of only those students who are having marks of math subject more than 85 so this is a complex scenario so where in you have to loop through every student and fetch subjects and out of this subject you should fetch only math subject and check the value of that marks whether it is greater than 85 so in such scenarios how you write the logic so we have already that descendant student so each student we have in that we will do a where condition so I'll say student and since my logic is little complex I cannot write it in one statement I want a multiple statement so I will start a parenthesis so if you know about this lambda expressions when you can write in one single statement and it returns a boolean then you can write it directly if not if you want to write multiple lines of code to resolve the logic then you can start a parenthesis and you can write how you write normally a method body but ultimately that particular body should return a bool output so now here I will write my logic so first work will be I want to fetch my subjects so for that I will say where subjects is equal to now I have st that is student element dot element which element I want to first fetch that is marks so I will fetch the marks and under marks I want to fetch all the elements named subjects so that again we will use descendants since there are multiple entries so which descendants we want with this subject so with this we will fetch all the subject elements so again this is an ienumerable with all these subjects so now on top of this I can still write this in one line but I am just breaking into multiple so that the logic is clear now I want to fetch only the maths subject so what I'll do is in this subject that is subjects dot I want to fetch only one since we know in each set of subjects in every student there will be only one element with the attribute maths so I will say first or default that is fetch me the first element which is having so-and-so condition so the condition will be again here it will be like maybe I can write sub I will define subject variable s sub and sub dot so now subject we have to find the attribute so that is nothing but attribute so which attribute we want we want the title attribute and this title attribute if we want to convert into string so if we convert into string we can compare it with maths so now this will fetch me it will loop through this subjects and it will look in each subject the attribute title and we'll try to match with their string maths so whichever matches the first occurrence of that will be returned so this I can take it into another variable may be like where maths subject equal to so now I have the maths subject also now what I want to compare in this logic is whether my maths subjects value is greater than 85 so again that yes or no that true or false should be returned for my this particular where condition because this expects a boolean so here I will do a return statement so return math subject it is math subject so now this math subject is the this particular entry so since we know that dot value will give you a string instead I will convert this particular x element itself into int and I will compare it with greater than 85 so now this logic will be correct so now I will repeat this logic once again for your understanding like we have the students data that is all these information in which we are fetching descendents of student that means one by one fetch me this information in each one we are having a where clause so that is we are having a filtering mechanism so now when we say where of some variable this variable will loop means this where will loop in this data and in this variable it will fetch me one by one student information so first of this one will come in to st then this one will come in to st then this one will come to st and so on so in each st that is in each student now we are having a little bigger logic where an in each student get me an element with marks so in this student it will go to the element marks and in that marks element get me the descendant subject so that means this particular information this particular all three student subjects will be taken out and put it into this so now in this list I want to first find the first element which matches this condition so what is the condition that each subject which is having an attribute title so here it can be multiple attributes like title fee is or duration whatever sex they can be many attributes out of which I want to find only one attribute which is title and in that attribute value I want to check whether it is maths or not so this is my condition so this whenever it matches with the any one element automatically that particular complete element will be returned here and then that particular element value I am converting into integer type and checking whether it is greater than 85 so this is the logic for this and in this case when you run now you will get the data of Rohan and Raj because you will see Rohan has maths marks 89 Raj has smart maths marks as 88 whereas this 82 and 85 so this logic will fetch me these two data so I hope this is clear so maybe you can practice this by maybe you can have an XML and in that XML data you can try to do different logics to find out different data you loop into the deeper elements and find out the logic so with that practice you will be able to use these X element so very easily so this is how we fetch the data and read it you can create your new XML data also so that we will see how we can do it and remove this code here and maybe this also I don't need anymore so first what we will do is like we will create a element so maybe where data so we have to create an XML data so that we can create by saying new X element so now X element expects first as name or you can pass multiple internal elements so first we will pass a name so like how I showed in the slide I can create persons so this is my this will be my root node so like how we have students node here like that this will be my first node so if you put comma that means whatever you give next will be under this particular node so under this element now you are telling the what are your child elements so this child element I will tab here so that it will be nicely arranged it will be again another new X element in this X element now I want individual person data so I can say person and now under person I want multiple tags so now again I have can put comma so again it will be inside the person element and here I will create new X element now this X element will be like for example person name but this name will not have inside any child element whereas it should have a value so instead of creating comma X new X element I can directly put the when you here so now maybe the value will be Raj and at this level itself I want another notes so I can put comma I can say a new X element and again here I can put another element so H comma maybe value is like 32 so now you will see this X element starts from here and ends here and it has three parameters first one this is the second one and this is the third one so this will be the parent and these two will be child under that but since these are at the same level in the same constructor so these two will be at the same level like how we have these at the same level so we will see the output very soon so maybe after this now I am at this level so I can create one more new excitement I can just copy this data completely and I can put it here and I will change this to maybe Tom and some data I'll remove this comma and I can close this so now you can see the route is persons under which there are person-to-person elements under which we have two more elements one is name one is age so this is the element structure which we have created and if you want to write it into an XML file you can just use any file operation like if you are aware of file this comes from system dot IO dot file so this will have file dot write alt text which takes two parameters one is string path and one string content so path I will give this same path so not not the same path I will take the same folder and I will say my data so some XML file and comma this data so data is coming from here so that will say data dot to string so if you see this data is of type X element you can just say two strings so it will convert into the complete string and the moment you run this I will show you the XML data folder so this is the folder where we don't have any file called my data and I will just run this so the execution is done and if you go to that folder you will see my data is available so I'll just drag it here to see the content now you can see here you have persons which is a root and which ends here inside which you have person-to-person details so that is nothing but this person and this person in each person element you have two more elements named name and age so this is how you can create it but this particular way of creating a element XML data is fine for small xml structures and all but not when it goes very complex so for very complex data I would always recommend XML serialization so that we will be covering in some other video which is not part of this link you that is a general dotnet concept so for writing any small XML data and all if you want to create on the fly on very quick in XML data so that you can use this X element way so I hope this was useful so now you know how using link you to XML how you can read XML data and how you can create XML data so if you like our content please do subscribe our channel and do like our videos if you have any feedback or any queries do put in the comments below the video and don't forget to hit bell icon so that you are notified for the future videos thank you
Info
Channel: CODELLIGENT
Views: 8,793
Rating: undefined out of 5
Keywords: LINQ, DotNet
Id: H6AvBDcTn3g
Channel Id: undefined
Length: 27min 57sec (1677 seconds)
Published: Sun Apr 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.