<Configuration xmlns="http://schemas.vurdalakov.net/linqtest">
<Plugins>
<Plugin Name="Watchdog" Type="Background"/>
<Plugin Name="UpdateChecker" Type="Background"/>
<Plugin Name="MainForm" Type="UI"/>
<Plugin Name="OptionsDialog" Type="UI"/>
<Plugin Name="AboutDialog" Type="UI"/>
</Plugins>
</Configuration>
Plugin information is read into list of the following class:public class Plugin
{
public String Name { get; set; }
public String Type { get; set; }
}
And the required code is:namespace LinqTest
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
static public class ParseXml
{
static public List<Plugin> Example4(String fileName, String type)
{
XDocument document = XDocument.Load(fileName);
XNamespace ns = document.Root.Name.Namespace;
List<Plugin> plugins = new List<Plugin>(from plugin in document.Descendants(ns + "Plugin")
where plugin.Attribute("Type").Value.Equals(type)
select new Plugin
{
Name = (String)plugin.Attribute("Name"),
Type = (String)plugin.Attribute("Type"),
});
return plugins;
}
}
}
All blog entries related to "Parse XML file with Linq":
- Parse XML file with Linq, part 1: Simple case
- Parse XML file with Linq, part 2: Adding namespaces
- Parse XML file with Linq, part 3: Complex structures
- Parse XML file with Linq, part 4: Filtering
- Source code of the whole test project
No comments:
Post a Comment