[C#] LINQ to XML Full Example 09-02-2012, 03:20 AM
#1
Here's an example of LINQ to XML that i've put together quick over the past few minutes. Hopefully you can see the power of LINQ and learn something from this along the way. Any questions just ask.
Let's start with a class here. I've got a few values, and for the sake of the demonstration i've also included a class member for a List of another class to show more XML values underneath when we hit the output:
Before we can deal with a collection of data here, we need to declare out List<T>:
In the first part of this, we're adding our Products to the data List. I add 2 of them here because it's quite a bit just to add one lol. But in the end you should be able to see what's going on here...:
In the query, we're basically selecting each of the members from the class instance's in the List, and for the List of the CountryAvailability class, we're going into that and selecting all of those member values and placing them into XML objects.
We use custom XML settings, and use an XMLWriter to write the output of the XML query to a StringBuilder, in which to later output the values to a textBox.
Here's what I ended up with as a result:
Let's start with a class here. I've got a few values, and for the sake of the demonstration i've also included a class member for a List of another class to show more XML values underneath when we hit the output:
Code:
public class Product
{
public int ID;
public string Name;
public int Stock;
public bool Available;
public List<CountryAvailability> Countries;
public class CountryAvailability
{
public DateTime DateAvailable;
public string CountryName;
}
}Before we can deal with a collection of data here, we need to declare out List<T>:
Code:
private List<Product> data = new List<Product>();In the first part of this, we're adding our Products to the data List. I add 2 of them here because it's quite a bit just to add one lol. But in the end you should be able to see what's going on here...:
Code:
data.Add(new Product()
{
ID = 2048,
Name = "ProductX",
Stock = 2500,
Available = true,
Countries = new List<Product.CountryAvailability>((new Product.CountryAvailability[]
{
new Product.CountryAvailability() {CountryName = "Canada", DateAvailable = new DateTime(2012, 12, 1)},
new Product.CountryAvailability() {CountryName = "United States", DateAvailable = new DateTime(2012, 12, 7)}
}))
});
data.Add(new Product()
{
ID = 1357,
Name = "ProductX10",
Stock = 170,
Available = true,
Countries = new List<Product.CountryAvailability>((new Product.CountryAvailability[]
{
new Product.CountryAvailability() {CountryName = "Canada", DateAvailable = new DateTime(2017, 12, 1)},
new Product.CountryAvailability() {CountryName = "United States", DateAvailable = new DateTime(2020, 1, 8)}
}))
});
var query = new XElement("Products",
from p in data
select
new XElement("Product",
new XAttribute("ProductID", p.ID),
new XAttribute("Name", p.Name),
new XAttribute("Stock", p.Stock),
new XAttribute("Available", p.Available),
from c in p.Countries
select new XElement("Availability",
new XAttribute("Country", c.CountryName),
new XAttribute("DateAvailable", c.DateAvailable))));
StringBuilder sb = new StringBuilder();
XmlWriterSettings XMLconfig = new XmlWriterSettings()
{
OmitXmlDeclaration = true,
IndentChars = "\t",
Indent = true
};
using (XmlWriter xw = XmlWriter.Create(sb, XMLconfig))
{
new XDocument(query).Save(xw);
}
//Output the value
textBox1.Text = sb.ToString();In the query, we're basically selecting each of the members from the class instance's in the List, and for the List of the CountryAvailability class, we're going into that and selecting all of those member values and placing them into XML objects.
We use custom XML settings, and use an XMLWriter to write the output of the XML query to a StringBuilder, in which to later output the values to a textBox.
Here's what I ended up with as a result:
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)