Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to read the Servers in a Serverlist from the XML file
and it shows me an error "error in xml document (2,2)
and I dont know how to fix it and if the xml file systax has an error ?


My Sourcecode:
C#
XmlSerializer serializer = new XmlSerializer(typeof(Server));
FileStream loadStream = new
FileStream(@"C:\Users\dominik.scholz\Desktop\Settings.xml",
FileMode.Open, FileAccess.Read);
Settings loadedObject = (Settings)serializer.Deserialize(loadStream);
loadStream.Close();


My Class named "Serverliste"
C#
[XmlRoot("Settings"), Serializable]
public class Settings
{
    [XmlElement("Serverliste")]
    public Serverliste Serverliste { get; set; }
}

public class Serverliste
{
    [XmlElement("Server")]
    public List<Server> Server { get; set; }
}

public class Server
{
    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlAttribute("Host")]
    public string Host { get; set; }

    [XmlElement("Port")]
    public int Port { get; set; }

    [XmlElement("Aktiv")]
    public bool Aktiv { get; set; }
}



and my XML File
XML
<?xml version="1.0"?>
<Settings>
    <Serverliste>
        <Server>
			<Name serializeAs="String">
				<value>Server1</value>
			</Name>
            <Host serializeAs="String">
                <value>127.0.0.1</value>
            </Host>
            <Port serializeAs="String">
                <value>10</value>
            </Port>
            <Aktiv serializeAs="boolean">
                <value>true</value>
            </Aktiv>
        </Server>
        <Server>
			<Name serializeAs="String">
				<value>Server2</value>
			</Name>
            <Host serializeAs="String">
                <value>192.168.0.1</value>
            </Host>
            <Port serializeAs="String">
                <value>20</value>
            </Port>
            <Aktiv serializeAs="boolean">
                <value>false</value>
            </Aktiv>
        </Server>
    </Serverliste>
</Settings>



What I have tried:

I tried to check xml file for errors, also tried to make use of Deserialize method.

Searched a lot and saw a lot of examples but didnt work for me, maybe the xml file sytax has an error?
Posted
Updated 5-Sep-18 22:45pm

1 solution

On the first look, Server class must be collection (array).

I'd suggest to follow instruction from: .net - Generate C# class from XML[^] to create proper set of classes with their attirbutes.

Please, read my tip too: A Complete Sample of Custom Class Collection Serialization and Deserialization[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900