Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey,

I have the problem, that I have to serialize multiple objects of the same type to a xml-file with the XmlSerializer-Class. But I'm only able to create xml-files like this one here:
XML
<Projects>
  <Name>A</Name>
  <TimeTotal>0</TimeTotal>
  <TimeToday>0</TimeToday>
  <Comment>a</Comment>
</Projects>
<Projects>
  <Name>B</Name>
  <TimeTotal>0</TimeTotal>
  <TimeToday>0</TimeToday>
  <Comment>b</Comment>
</Projects>

But thats invalid xml. So i want that it looks like this:
XML
<TimeProjects>
  <Projects>
    <Name>A</Name>
    <TimeTotal>0</TimeTotal>
    <TimeToday>0</TimeToday>
    <Comment>a</Comment>
  </Projects>
  <Projects>
    <Name>B</Name>
    <TimeTotal>0</TimeTotal>
    <TimeToday>0</TimeToday>
    <Comment>b</Comment>
  </Projects>
</TimeProjects>

So the problem is that I don't know how to add ONLY ONE TIME a root element and then multiple sub-elements.

Thats my code how I try to add objects to a xml-file:
C#
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TimeProjects));
ProjectWriter = new StreamWriter(filePath, true);
        
TimeProjects project = new TimeProjects { Name = projectName, Comment = comments, TimeToday = timeToday, TimeTotal = timeTotal };

xmlSerializer.Serialize(ProjectWriter, project);

ProjectWriter.Close();

I hope you can help me. If you need more informations with that, just ask me please!!
Posted

Should be fairly simple.
Try creating the XmlSerializer with the constructor that also takes XmlRootAttribute

C#
// Fix this line of code, use the costructor that follows : XmlSerializer(Type, XmlRootAttribute)
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TimeProjects));

// try using: XmlSerializer xmlSerializer = new XmlSerializer(typeof(TimeProjects), new XmlRootAttribute("TimeProjects"));


Cheers,
C
 
Share this answer
 
v4
Comments
ToShX 27-Nov-14 4:04am    
Works fine. Thank you a lot!
How about not dealing with XML manually at all? The best XML serialization is implemented using the Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA
 
Share this answer
 
Comments
ToShX 27-Nov-14 4:06am    
Thanks for your answer. had a lot to read :P
But for my situation _Canny_Brisk_'s solution works fine for me. But I guess in time I have to go back to your solution.
Sergey Alexandrovich Kryukov 27-Nov-14 4:19am    
I would highly recommend that...
—SA
ToShX 27-Nov-14 4:22am    
yeah, please know that I just started my education as a software developer. So i have to learn these things step by step. Yet I don't know whats going on with all these metadatas everyone suggests, so I have to take the easiest answer I can get, if they're working for me :)
Hi Please try this Code

C#
public class MakeSer
    {

        public MakeSer()
        {
            TimeProject tm = new TimeProject();
            Projects[] proj = new Projects[2];
            proj[0] = new Projects { Comment = "X", name = "Y", TimeToday = "40", TimeTotal = "10" };
            proj[1] = new Projects { Comment = "XX", name = "CY", TimeToday = "50", TimeTotal = "30" };


            tm.TimeProjects = proj;
            System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(tm.GetType());
            StringBuilder builder = new StringBuilder();
            using (StringWriter writer = new StringWriter(builder))
            {
                xml.Serialize(writer, tm);
            }
            string value_ = builder.ToString();

        }
    }

    [Serializable]
    public class Projects
    {
        public string name { get; set; }
        public string TimeTotal { get; set; }
        public string TimeToday { get; set; }
        public string Comment { get; set; }
    }
    [Serializable]
    public class TimeProject
    {
      
        public Projects[] TimeProjects { get; set; }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900