C# : The best solution to parse the xml file?
I can write this employee object's information into the XML file. Look like this.
Employee's class
class Employee
{
int _id;
string _firstName;
string _lastName;
int _salary;
string _text;
public Employee(int id, string firstName, string lastName, int salary,string text)
{
this._id = id;
this._firstName = firstName;
this._lastName = lastName;
this._salary = salary;
this._text = text;
}
public int Id { get { return _id; } }
public string FirstName { get { return _firstName; } }
public string LastName { get { return _lastName; } }
public int Salary { get { return _salary; } }
public string Text { get { return _text; } }
}
Writen Xml.
<Employees>
<Employee Text="text1">
<ID>1</ID>
<FirstName>David</FirstName>
<LastName>Smith</LastName>
<Salary>10000</Salary>
</Employee>
<Employee Text="text2">
<ID>3</ID>
<FirstName>Mark</FirstName>
<LastName>Drinkwater</LastName>
<Salary>30000</Salary>
</Employee>
<Employees>
Then I want to parse xml back into Employee object, What's the best solu开发者_开发技巧tion?
Personally, I'd use LINQ to XML:
XDocument doc = XDocument.Load("employees.xml");
var employees = doc.Root.Elements("Employee")
.Select(e => new Employee(
(int) e.Attribute("ID"),
(string) e.Attribute("FirstName"),
(string) e.Attribute("LastName"),
(int) e.Attribute("Salary"))
.ToList();
Note that this isn't currently performing any validation that the attributes exist. If you need more complicated validation and construction, you could change the query to something like:
XDocument doc = XDocument.Load("employees.xml");
var employees = doc.Root.Elements("Employee")
.Select(Employee.FromXElement)
.ToList();
where Employee.FromXElement
would be a static method which knows the details of how to create an Employee from an XElement
. You might then want an instance method of ToXElement
to perform the reverse operation. I've used this pattern before and had great success with it.
While you can certainly use XML serialization, I find this tends to be less flexible than "custom" serialization, in terms of the output it can produce, the input it can accept, and allowing you to cope with an evolving set of data in the type. As Darin's shown, it also puts various requirements on the type which may not fit in with the rest of your design - such as your pleasantly-immutable current type.
On the other hand, the built-in XML serialization will cope better at things like referential integrity.
You could use XmlSerializer.
var root = new XmlRootAttribute
{
ElementName = "Employees"
};
var serializer = new XmlSerializer(typeof(Employee[]), root);
using (var reader = XmlReader.Create("employees.xml"))
{
var employees = (Employee[])serializer.Deserialize(reader);
}
Slight modification is required to your Employee class for this to work:
- It needs to be public
- It needs to have default constructor
The properties need to have setters
public class Employee { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Salary { get; set; } }
精彩评论