Kestami
Kestami

Reputation: 2073

Deserialising XML to List

After many hours of trying to get a list to serialize to XML, and completing this (although I'm sure I did it very shoddily). I need to now deserialize the XML back into a list. Here is my XML file for starters.

 <?xml version="1.0" encoding="utf-8"?>
<ArrayOfPrograms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Programs>
    <File-name>chrome.exe</File-name>
    <File-path>C:\Users\Shane\AppData\Local\Google\Chrome\Application\chrome.exe</File-path>
  </Programs>
  <Programs>
    <File-name>lol.launcher.exe</File-name>
    <File-path>C:\Riot Games\League of Legends\lol.launcher.exe</File-path>
  </Programs>
</ArrayOfPrograms>

And heres my config class I'm using;

  public class Config
{
    [XmlElement("Recipient")]
    public string recipient;
    [XmlElement("Username")]
    public string username;
    [XmlElement("Password")]
    public string password;
    [XmlElement("Serial-ID")]
    public string serialId;


[XmlElement("Email-settings")]

    public Email Emails { get; set; }

    [XmlArray("Program-List"), XmlArrayItem(typeof(Programs), ElementName = "Programs")]
    public List<Programs> Programs { get; set; }

    [XmlElement("Database-settings")]
    public Database Databases { get; set; }
}

 public class Programs
{
    public string filename;
    public string filepath;
    [XmlElement("File-name")]
    public string Filename { get; set; }
    [XmlElement("File-path")]
    public string Filepath { get; set; }

}

Can anyone give me any ideas as to which route I should be heading in? I've never dealt with deserializing into lists.

Upvotes: 0

Views: 547

Answers (4)

ADIMO
ADIMO

Reputation: 1117

You can use Linq to XML and write code like this:

public class MyProgram
{
    public string FileName { get; set; }
    public string FilePath { get; set; }
}

// load data file
using ( XmlTextReader xmlReader = new XmlTextReader("myfilename.xml") )
{
    XDocument xdoc = XDocument.Load(xmlReader);

    var programs= from programItem in xdoc.Root.Elements()
                select new MyProgram {
                    FileName = programItem.Element("File-name").Value,
                    FilePath = programItem.Element("File-path").Value
                };

    List<MyProgram> result = programs.ToList();
}

Upvotes: 1

L.B
L.B

Reputation: 116098

using (var f = File.Open("...", FileMode.Open))
{
    XmlSerializer serializer = new XmlSerializer(typeof(List<Programs>));
    List<Programs> programs = (List<Programs>)serializer.Deserialize(f);
}

Upvotes: 3

Mikey Mouse
Mikey Mouse

Reputation: 3088

There's this great tool I use for that kind of thing that comes with Visual Studio called XSD

http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.80).aspx

Basically you point it at an XML file, then it builds your C# file. You import that into your solution and you can use the object as if it was an XML file and simply serialize or deserialize to convert between the two.

Edit:

Oh to use it, just click Start, Programs, Visual Studio (2008 or 2010), Visual Studio Tools, then the Command Prompt.

Then just head over to the folder you XML file is in and "XSD /l cs myFile.xml" for C# code that is

Upvotes: 1

hkutluay
hkutluay

Reputation: 6944

Easiest way for deserializing xml is using XML Schema Definition Tool (Xsd.exe)

xml schema definition tool

Upvotes: 0

Related Questions