1

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?

3 Answers 3

1

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)
                     });
 }
Sign up to request clarification or add additional context in comments.

Comments

1

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);
} 

Comments

1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.