0

I have a xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Devices>
        <Settings>
            <Name>ABC</DeviceName>
            <HostNic>LAN_1</HostNic>
        </Settings>
    </Devices>
</Configuration>

I want to deserialize this to object form. I am trying to define a class structure for the xml file like this:

class Configuration
{
    [XmlElement("Address")]
    public List<Devices> deviceList = new List<Devices>();
}

class Devices
{
    [XmlElement("Address")]
    public List<Settings> settingList = new List<Settings>();
}

class Settings
{
    public string Name { get; set; }
    public string HostNic { get; set; }
}

Is there any other appropriate way of defining class for this xml file?

kara
  • 3,205
  • 4
  • 20
  • 34
Harshith R
  • 418
  • 1
  • 11
  • 31
  • I think you need an `XmlRoot` somewhere in there. – Blue Jul 04 '19 at 05:02
  • 2
    There are some helpful information [Generate C# class from XML](https://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml), this may help you. – csharpbd Jul 04 '19 at 05:12
  • The classes must be public to be serialized by `XmlSerializer`. Beyond that, what is your question? Do you have a problem? Is something not working? – dbc Jul 04 '19 at 05:22
  • @dbc It is working. I was just wondering is there any more effiecient way of writing class for this xml file. – Harshith R Jul 04 '19 at 05:42
  • Your XML is not well-formed. In `ABC` the end element name does not match the start element name. Upload to https://www.xmlvalidation.com/ for confirmation. – dbc Jul 04 '19 at 05:43
  • Actually it's not working. `[XmlElement("Address")]` in `Configuration` should be `[XmlElement("Devices")]` and `[XmlElement("Address")]` in `Devices` should be `[XmlElement("Settings")]`. And all classes must be public to be serialized by `XmlSerializer`. See https://dotnetfiddle.net/zQygFi for working classes and well-formed XML. So, can you clarify what your question is? – dbc Jul 04 '19 at 05:46
  • 1
    You may want to clarify what efficiency you are after? Do you mean less code or efficient (de)serialisation? It may also help to give more context about the constraints you have. – Sudhanshu Mishra Jul 04 '19 at 06:02

1 Answers1

1

Your Classes need some modifications, specially the attributes you have added.

[XmlRoot]
public class Configuration
{
    [XmlElement("Devices")]
    public List<Devices> deviceList = new List<Devices>();
}

public class Devices
{
    [XmlElement("Settings")]
    public List<Settings> settingList = new List<Settings>();
}

public class Settings
{
    public string Name { get; set; }
    public string HostNic { get; set; }
}

Then you can de-serialize the XML into the above classes:

var serializer = new XmlSerializer(typeof(Configuration));
using (System.IO.TextReader reader = new System.IO.StringReader(<Your XML String>))
{
     Configuration config = (Configuration)serializer.Deserialize(reader);
}
Arif Eqbal
  • 3,068
  • 1
  • 18
  • 10