Now that we've seen how to create a basic XML tree and the basic classes of LINQ to XML and its hierarchy, let's take a more detailed look at loading XML data and finding our way through it.
There are different ways of loading XML data. The XML data can be in the form of a file, or a string, or any other supported formats. Using LINQ to XML, we can load XML data through the XElement
object. For example, to load XML data from a file, we can use the Load
method of the XElement
object, as given below:
// Loading XML XElement LoadIcecreams = XElement.Load(@"c:\Icecreams.xml");
If the XML data is in the form of a string, we can load it through XElement
by using the Parse
method. For example, the code for loading Icecreams
from string is as follows:
XElement LoadIcecreamsfromString = XElement.Parse( @"<Icecream> <Name>Rum Raisin Icecream</Name> <Cholesterol>49mg</Cholesterol> <VitaminA type=""VitaminA"">2g</VitaminA>...