C#Object-Serialization和-Deserialized in XML [英] C# Object-Serialization and -Deserialization in XML

查看:73
本文介绍了C#Object-Serialization和-Deserialized in XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我有问题,我必须使用XmlSerializer-Class将多个相同类型的对象序列化为xml文件。但是我只能在这里创建像这样的xml文件:

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:

<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>



但是这会使xml无效。所以我希望它看起来像这样:


But thats invalid xml. So i want that it looks like this:

<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>



所以问题是我不知道如何添加仅限O NE TIME 一个根元素,然后是多个子元素。



这就是我的代码如何尝试将对象添加到xml文件中:


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:

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!!

推荐答案

应该相当简单。

尝试创建XmlSerializer构造函数也需要 XmlRootAttribute



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

// 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"));





干杯,

C



Cheers,
C


如何不手动处理XML?最好的XML序列化是使用数据协定实现的:

http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



请参阅我过去的答案,我提倡这种方法:

如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ],

创建属性文件...... [ ^ ]。



-SA
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


您好请试用此代码



Hi Please try this Code

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; }
    }


这篇关于C#Object-Serialization和-Deserialized in XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆