toffaste1337
toffaste1337

Reputation: 35

Load xml from file

Been looking around alot to find answers to my problem. I found a great way to convert my List to an xml file, but I also want to get it back to a list.

My current xml file looks like this:

<Commands>
    <Command command="!command1" response="response1" author="niccon500" count="1237"/>
    <Command command="!command2" response="response2" author="niccon500" count="1337"/>
    <Command command="!command3" response="response3" author="niccon500" count="1437"/>
</Commands>

Generated with:

var xml = new XElement("Commands", commands.Select(x =>
                      new XElement("Command",
                      new XAttribute("command", x.command),
                      new XAttribute("response", x.response),
                      new XAttribute("author", x.author),
                      new XAttribute("count", x.count))));
            File.WriteAllText(file_path, xml.ToString());

So, how would I get the info back from the xml file?

Upvotes: 1

Views: 219

Answers (3)

YuvShap
YuvShap

Reputation: 3835

You can use XDocument.Load method to load the document and then create command list using LINQ to XML syntax :

XDocument doc = XDocument.Load(file_path);
var commands = doc.Root.Elements("Commands").Select(e => 
                      new Command()
                      {
                        command = e.Attribute("command").Value, 
                        response = e.Attribute("response").Value,
                        author = e.Attribute("author").Value, 
                        count= int.Parse(e.Attribute("count").Value) 
                      }).ToList();

Upvotes: 1

Giorgos Betsos
Giorgos Betsos

Reputation: 72205

You can use the following piece of code:

XDocument doc = XDocument.Load(@"myfile.xml");

to load the XML file into an XDocument object.

Then you can use .Descendants("Commands").Elements() to access all elements contained inside the <Commands> element and add them to a list:

 List<Command> lstCommands = new List<Command>();
 foreach(XElement elm in doc.Descendants("Commands").Elements())
 {               
     lstCommands.Add(new Command
                     {
                        command = elm.Attribute("command").Value,
                        response = elm.Attribute("response").Value,
                        author = elm.Attribute("author").Value,
                        count = int.Parse(elm.Attribute("count").Value)
                     });
 }

Upvotes: 1

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22094

When using xml for persistent storage, start with defining POC classes and decorate them with proper attributes - like

[XmlType("Command")]
public class CommandXml
{
    [XmlAttribute("command")]
    public string Command { get; set; }
    [XmlAttribute("response")]
    public string Response { get; set; }
    [XmlAttribute("author")]
    public string Author { get; set; }
    [XmlAttribute("count")]
    public int Count { get; set; }
}

[XmlRoot("Commands")]
public class CommandListXml : List<CommandXml>
{
}

And then de-serialization is simple as:

var txt = @"<Commands>
    <Command command=""!command1"" response=""response1"" author=""niccon500"" count=""1237""/>
    <Command command=""!command2"" response=""response2"" author=""niccon500"" count=""1337""/>
    <Command command=""!command3"" response=""response3"" author=""niccon500"" count=""1437""/>
</Commands>";
CommandListXml commands;
using (var reader = new StringReader(txt))
{
    var serializer = new XmlSerializer(typeof(CommandListXml));
    commands = (CommandListXml)serializer.Deserialize(reader);
} 

Upvotes: 1

Related Questions