Reputation: 33
My XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServerConfig loggingEnabled="1">
<Servers id="2" host="mytest">
<Server host="test1.example.com" port="9999" >
<Name id="newname1">
<FirstName id="myfirstname">hello first name</FirstName>
<SecondName id="myfirstname">hello first name</SecondName>
</Name>
</Server>
<Server host="test2.example.com" port="8888" />
</Servers>
<Servers id="1">
<Server host="test3.example.com" port="9899" >
<Name id="newname2">
<FirstName id="myfirstname">hello first name</FirstName>
</Name>
</Server>
<Server host="test4.example.com" port="8988" />
</Servers>
</ServerConfig>
I want to deserialize this xml to my class
Class
public sealed class ServerConfig
{
public sealed class Server
{
[XmlAttribute("host")]
public string Host { get; set; } // gives me host name
[XmlAttribute("port")]
public int Port { get; set; } // gives my prot number
}
[XmlArray]
public List<Server> Servers { get; set; } // gives me all 4 server lsit
[XmlAttribute("loggingEnabled")]
public int LoggingEnabled { get; set; } // gives me attribute detail
public ServerConfig()
{
Servers = null;
LoggingEnabled = 0;
}
}
Problem
My Problem is I don't know how to access the Attributes of the nested Element Name and sub nested element FirstName/SecondName
WIll be really very thankful.
Thanks.
Upvotes: 0
Views: 55
Reputation: 33
The simplest way is being provided by Microsoft in VS 2012 onwards which is being brilliantly explained in this link.
All you need to do is copy your xml and then paste special to a class as explained in the link.
Happy Coding.
Thanks,
Upvotes: 1