Hello everyone. I'm wrestling 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 so in the most elegant and succinct way using LINQ-to-XML, but given that I'm not the smartest kid in town when it comes to nested methods, infered types, generics, et cétera, I thought it'd be a good idea to ask if any of you guys would like to go ahead and show me some LINQ literacy :)
Right now for every 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 do know I'm totally missing the point, not only because I'm repeating myself a lot there but also because that queries only work when there's only one item so, again if you have any comment or suggestion it would be really appreciated. Thanks much in advance!
UPDATE: in case that perhaps the former example wasn't really helpful, here's a more sort of realistic scenario:
<?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>
Therefore for each of those items I'd like to generate variables so I can pass them as parameters for some FTP management method.
SOLVED:
It was as easy as doing a 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
);
}