Working with LINQ to XML
LINQ to XML is a LINQ provider that allows you to query and manipulate XML.
Let's create a method to convert the Products table into XML:
- In the
LinqWithEFCore
project, inProgram.Functions.cs
, import theSystem.Xml.Linq
namespace, as shown in the following code:
using System.Xml.Linq; // To use XElement, XAttribute.
- In
Program.Functions.cs
, add a method to output the products in XML format, as shown in the following code:
static void OutputProductsAsXml()
{
SectionTitle("Output products as XML");
using (NorthwindDb db = new())
{
Product[] productsArray = db.Products.ToArray();
XElement xml = new("products",
from p in productsArray
select new XElement("product",
new XAttribute("id", p.ProductId),
new XAttribute("price", p.UnitPrice ?? 0),
new XElement("name", p.ProductName)));
WriteLine(xml.ToString());
}
}
- In
Program.cs
, call theOutputProductsAsXml
method. - Run the project, view the result, and note that the structure of the XML generated matches the elements and attributes that the LINQ to XML statement declaratively described in the preceding code, as shown in the following partial output:
<products>
<product id="1" price="18">
<name>Chai</name>
</product>
<product id="2" price="19">
<name>Chang</name>
</product>
...
You might want to use LINQ to XML to easily query or process XML files:
- In the
LinqWithEFCore
project, add a file namedsettings.xml
. - Modify its contents, as shown in the following markup:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="color" value="red" />
<add key="size" value="large" />
<add key="price" value="23.99" />
</appSettings>
Warning! If you are using Visual Studio 2022, then the compiled application executes in the
LinqWithEFCore\bin\Debug\net8.0
folder so it will not find thesettings.xml
file unless we indicate that it should always be copied to the output directory. Select thesettings.xml
file and set its Copy to Output Directory property to Copy always.
- In
Program.Functions.cs
, add a method to complete these tasks, as shown in the following code:- Load the XML file.
- Use LINQ to XML to search for an element named
appSettings
and its descendants namedadd
. - Project the XML into an array of an anonymous type with
Key
andValue
properties. - Enumerate through the array to show the results:
static void ProcessSettings()
{
string path = Path.Combine(
Environment.CurrentDirectory, "settings.xml");
WriteLine($"Settings file path: {path}");
XDocument doc = XDocument.Load(path);
var appSettings = doc.Descendants("appSettings")
.Descendants("add")
.Select(node => new
{
Key = node.Attribute("key")?.Value,
Value = node.Attribute("value")?.Value
}).ToArray();
foreach (var item in appSettings)
{
WriteLine($"{item.Key}: {item.Value}");
}
}
- In
Program.cs
, call theProcessSettings
method. - Run the console app and view the result, as shown in the following output:
Settings file path: C:\cs12dotnet8\Chapter11\LinqWithEFCore\bin\Debug\net8.0\settings.xml
color: red
size: large
price: 23.99