TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
abdelwaheb ammar
1.3k
457
123.9k
serialize a list of object
May 13 2020 4:36 AM
hi, i want to serialize an objects to xml file of a Departement contain a list of Employees.this is my code C# which i putted:
public
class
Department
{
public
string
Name {
get
;
set
; }
public
List<Employee> Employees {
get
;
set
; }
public
Department()
{
Employees =
new
List<Employee>();
}
}
public
class
Employee
{
public
string
Name {
get
;
set
; }
public
Employee() { }
public
Employee(
string
name)
{
Name = name;
}
}
Department dept =
new
Department();
dept.Name =
"IT"
;
dept.Employees.Add(
new
Employee(
"Bob"
));
dept.Employees.Add(
new
Employee(
"Jim"
));
dept.Employees.Add(
new
Employee(
"Mel"
));
XmlSerializer serializer =
new
XmlSerializer(dept.GetType());
using
(StreamWriter writer =
new
StreamWriter(@
"d:\Department.xml"
))
{
serializer.Serialize(writer, dept);
}
this code generate succefully a file xml like this:
<
Department
>
<
Name
>
IT
</
Name
>
<
Employees
>
<
Employee
>
<
Name
>
Bob
</
Name
>
</
Employee
>
<
Employee
>
<
Name
>
Jim
</
Name
>
</
Employee
>
<
Employee
>
<
Name
>
Mel
</
Name
>
</
Employee
>
</
Employees
>
</
Department
>
but my goal is to get that like this:
<
Department
>
<
Name
>
IT
</
Name
>
<
Employee
>
<
Name
>
Bob
</
Name
>
</
Employee
>
<
Employee
>
<
Name
>
Jim
</
Name
>
</
Employee
>
<
Employee
>
<
Name
>
Mel
</
Name
>
</
Employee
>
</
Department
>
that's mean i want to erase the <Employees> Element
how can i fix this?
Reply
Answers (
2
)
How to use HTML tags in word using console application
Write a Web API in c# to extract the data from a website