Click here to Skip to main content
15,899,679 members
Home / Discussions / C#
   

C#

 
GeneralRe: generating XML file issue Pin
Wendelius29-Nov-08 1:39
mentorWendelius29-Nov-08 1:39 
GeneralRe: generating XML file issue Pin
George_George30-Nov-08 3:59
George_George30-Nov-08 3:59 
GeneralRe: generating XML file issue Pin
Wendelius30-Nov-08 4:24
mentorWendelius30-Nov-08 4:24 
GeneralRe: generating XML file issue Pin
George_George30-Nov-08 18:32
George_George30-Nov-08 18:32 
GeneralRe: generating XML file issue Pin
Wendelius30-Nov-08 18:34
mentorWendelius30-Nov-08 18:34 
AnswerRe: generating XML file issue [modified] Pin
darkzangel29-Nov-08 10:15
darkzangel29-Nov-08 10:15 
GeneralRe: generating XML file issue Pin
George_George30-Nov-08 18:26
George_George30-Nov-08 18:26 
AnswerRe: generating XML file issue Pin
N a v a n e e t h29-Nov-08 16:21
N a v a n e e t h29-Nov-08 16:21 
You can use XMLSerialization to do this. It makes your code more cleaner than creating XML using XMLDocument.

All you need is to create types for representing DepartmentInfo, Club and Teacher. It could be something like the following
public class DepartmentInfo
{
    public DepartmentInfo(int id,string location,string type) {
        this.Id = id;
        this.Location = location;
        this.Type = type;
    }
    public DepartmentInfo() {

    }
    [XmlAttribute("Id")]
    public int Id { get; set; }
    public string Location { get; set; }
    public string Type { get; set; }
}

public class Club
{
    public Club(int id,string name) {
        this.Id = id;
        this.Name = name;
    }
    public Club() {

    }
    [XmlAttribute("Id")]
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Teacher
{
    public Teacher(int id,string name,string major) {
        this.Id = id;
        this.Name = name;
        this.Major = major;
    }
    public Teacher() {

    }
    [XmlAttribute("Id")]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Major { get; set; }
}
Now you need a Department type which has references to all the above shown types.
[XmlRoot("Department")]
    public class Department
    {
        public Department(DepartmentInfo info,Club[] clubs,Teacher[] teachers) {
            this.DepartmentInfo = info;
            this.Clubs = clubs;
            this.Teachers = teachers;
        }

        public Department() {
        }

        [XmlElement("DepartmentInfo")]
        public DepartmentInfo DepartmentInfo { get; set; }
        [XmlArray("Clubs")]
        public Club[] Clubs { get; set; }
        [XmlArray("Teachers")]
        public Teacher[] Teachers { get; set; }
    }
You can serialize the Department class to get the specified XML. Here is how you do in a console application.
static void Main(string[] args) {
    Club[] clubs = { new Club(200, "Football"), new Club(201, "Basketball") };
    Teacher[] teachers = { new Teacher(300, "Thomas", "Algorithm"), new Teacher(301, "Zhou", "Linear Programming") };
    Department department = new Department(new DepartmentInfo(100,"San Jose", "Computer Science"), clubs, teachers);

    XmlSerializer serializer = new XmlSerializer(typeof(Department));
    serializer.Serialize(Console.Out, department);
    Console.Read();
}
This method works well and your code will look cleaner. But serialization is performance costly as it uses reflection to get details of the type. Manually writing XML using XMLTextWriter or XMLDocument will give you better performance than this. Choosing the method is completly depends on your situation.

Hope this helps


GeneralRe: generating XML file issue Pin
George_George30-Nov-08 18:27
George_George30-Nov-08 18:27 
QuestionProperties and User Ma... Pin
jas0n2328-Nov-08 20:38
jas0n2328-Nov-08 20:38 
AnswerRe: Properties and User Ma... Pin
Wendelius28-Nov-08 22:29
mentorWendelius28-Nov-08 22:29 
AnswerRe: Properties and User Ma... Pin
Dave Kreskowiak29-Nov-08 4:59
mveDave Kreskowiak29-Nov-08 4:59 
GeneralRe: Properties and User Ma... Pin
jas0n2329-Nov-08 5:14
jas0n2329-Nov-08 5:14 
Question[Message Deleted] Pin
jas0n2328-Nov-08 19:33
jas0n2328-Nov-08 19:33 
AnswerRe: URL Regular Expression f... Pin
AhsanS28-Nov-08 20:36
AhsanS28-Nov-08 20:36 
GeneralRe: URL Regular Expression f... Pin
jas0n2328-Nov-08 20:40
jas0n2328-Nov-08 20:40 
AnswerRe: URL Regular Expression f... Pin
Guffa28-Nov-08 20:36
Guffa28-Nov-08 20:36 
AnswerRe: URL Regular Expression f... Pin
Garth J Lancaster28-Nov-08 23:49
professionalGarth J Lancaster28-Nov-08 23:49 
QuestionConverting byte[] to a stream Pin
darkzangel28-Nov-08 18:17
darkzangel28-Nov-08 18:17 
GeneralRe: Converting byte[] to a stream Pin
Luc Pattyn28-Nov-08 18:32
sitebuilderLuc Pattyn28-Nov-08 18:32 
GeneralRe: Converting byte[] to a stream Pin
darkzangel29-Nov-08 9:32
darkzangel29-Nov-08 9:32 
GeneralRe: Converting byte[] to a stream Pin
Luc Pattyn29-Nov-08 10:35
sitebuilderLuc Pattyn29-Nov-08 10:35 
Questiontime-division multiplexing Pin
Member 391904928-Nov-08 7:26
Member 391904928-Nov-08 7:26 
AnswerRe: time-division multiplexing Pin
User 665828-Nov-08 11:05
User 665828-Nov-08 11:05 
AnswerRe: time-division multiplexing Pin
Guffa28-Nov-08 11:10
Guffa28-Nov-08 11:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.