Literate LINQ-to-XML: Best practice for attribute / value deserialization for structured variable / value pairs

I am struggling to deserialize the following XML:

<?xml version="1.0" encoding="utf-8" ?>

<conf name="settings">

    <item name="lorem"
        one="the"
        two="quick"
        three="brown"
        four="fox"
        five="jumps"
        six="over"
        seven="the"
        eight="lazy"
        nine="dog"
            />

    <item name="ipsum"
        one="how"
        two="many"
        three="roads"
        four="must"
        five="a"
        six="man"
        seven="walk"
        eight="down"
        nine="?"
            />

</conf>

hoping to do it in the most elegant and concise way using LINQ-to-XML, but considering that I'm not the smartest guy in town when it comes to nested methods, derived types, generics and et cΓ©tera, I thought it would be nice to ask would you guys like to show me some LINQ literacy :)

Right now, for each value, I'm doing something like:

XDocument config = XDocument.Load("whatever.conf");

var one = from q in config.Descendants("item")
            select (string)q.Attribute("one");

var two = from q in config.Descendants("item")
            select (string)q.Attribute("two");

And I know that I absolutely do not understand the point, not only because I repeat a lot, but also because these requests only work when there is only one element, so if you have any comments or suggestions, be really appreciated. Many thanks!

:, , , , :

<?xml version="1.0" encoding="utf-8" ?>

<conf name="ftp-settings" freq="daily" time="23:00">
    <item   name="isis"
            host="10.10.1.250"
            user="jdoe"
            pass="4/cB0kdFGprXR/3oTs8mtw=="
            file="backup.tar.gz"
            path="/var/log"
        />
    <item   name="seth"
            host="10.10.2.250"
            user="jdoe"
            pass="4/cB0kdFGprXR/3oTs8mtw=="
            file="backup.tar.gz"
            path="/var/log"
        />
</conf>

, FTP.

:

, foreach:

var elements = from element in xml.Descendants("item") select element;

foreach (XElement item in elements) {
    ftp.DownloadFile(
        item.Attribute("host").Value,
        item.Attribute("user").Value,
        item.Attribute("pass").Value,
        item.Attribute("file").Value,
        item.Attribute("path").Value
        );
}
+5
2

, "" "" . , - :

var items = config.Descendants("item")
                  .Select(element => Item.FromXElement(element));

, " " ( Item.FromXElement), . Item LINQ to XML, -, :)

items - IEnumerable<Item>, - , .

, , - , . , " " ...


Item.FromXElement :

public static Item FromXElement(XElement element)
{
    string name = (string) element.Attribute("name");
    string host = (string) element.Attribute("host");
    // etc
    return new Item(name, host, ...);
}

(, , .)

+3

:

XDocument config = XDocument.Load("whatever.conf");

var pairs = from q in config.Descendants("item")
           select new {
               One = (string)q.Attribute("one"),
               Two = (string)q.Attribute("two") };

var ones = pairs.Select(pair => pair.One);
var twos = pairs.Select(pair => pair.Two);
+1

All Articles