Reputation: 5992
I have this class and xml file which i am using to get the values.
<Task>
<Employee Id="123">
<string>/Name</string>
<string>/Company</string>
</Employee>
<Manager Id="456">
<string>/Name</string>
<string>/Company</string>
</Manager>
</Task>
public class Task
{
public List<string> Employee;
public List<string> Manager;
}
var taks = (Task)new XmlSerializer(typeof(Task)).Deserialize(streamReader);
so, in tasks i am getting list of Employee with Name and Compnay as values correctly. I want to get the Id for each element. how do i get it?
/Name and /Company can be anything. I can put any value in there and I get it in employee without even creating a property for it. same goes for Manager as well, I can have /Email, /Website, /LastLogin etc and I get it in the Manager object without even creating a property for it.
appreciate your time and help.
Upvotes: 1
Views: 1224
Reputation: 1616
Define your Task
class as follows:
public class Task
{
public Employee Employee;
public Manager Manager;
}
Where Employee
is:
public class Employee
{
[XmlAttribute]
public string Id {get;set;}
public string Name{get;set;}
public string Company{get;set;}
}
And Manager
is:
public class Manager
{
[XmlAttribute]
public string Id {get;set;}
public string Name{get;set;}
public string Company{get;set;}
}
If you're interested in a generic list of properties for Employee
and/or Manager
, then consider having another class called Property
as the following:
public class Property
{
[XmlAttribute]
public string Value{get;set;}
}
Then, change your Manager
and Employee
as the following:
public class Employee
{
[XmlAttribute]
public string Id {get;set;}
public List<Property> Properties {get;set;}
}
public class Manager
{
[XmlAttribute]
public string Id {get;set;}
public List<Property> Properties {get;set;}
}
Finally, change your XML as the following:
<Task>
<Employee Id="123">
<Properties>
<Property Value="/Name" />
<Property Value="/Company"/>
</Properties>
</Employee>
<Manager Id="456">
<Properties>
<Property Value="/Name" />
<Property Value="/Company"/>
</Properties>
</Manager>
</Task>
Upvotes: 3