XMLSerialize 一个 ObservableCollection

发布于 2024-12-22 21:05:10 字数 1465 浏览 1 评论 0原文

我在可观察集合的 xml 序列化中遇到问题。

这是我要序列化的内容:

public enum Status { Pending, Active, Completed, Cancelled }

public abstract class Entity : INotifyPropertyChanged
{
    ...
}

public class UserStory : Entity
{
    public uint StoryID { get; set; }
    public Status Status { get; set; }
    ...
    public ObservableCollection<Task> Tasks { get; set; }
}

public class Task : Entity
{
    public uint TaskID { get; set; }
    ...
}

这是我序列化它的方式:

public static void SerializeObjectToXML<T>(T item, string FilePath)
{
    XmlSerializer xs = new XmlSerializer(typeof(T));
    using (StreamWriter wr = new StreamWriter(FilePath))
    {
        xs.Serialize(wr, item);
    }
}

public class Main()
{
    ObservableCollection<UserStory> UserStories { get; set; }

    void Main()
    {
        ...
        ObservableCollection<object> Document = new ObservableCollection<object>();
        Document.Add(UserStories);
        SerializeObjectToXML<ObservableCollection<object>>(Document , "...");
        ...
    }
}

但是 xs.Serialize(wr, item); 行中出现错误:

InvalidOperation 异常:生成 XML 文档时出错。 内部异常:类型 ScrumPresentor.ObservableCollection`1[[ScrumPresentor.UserStory, ScrumPresentor,版本=1.0.0.0,文化=中立, PublicKeyToken=null]] 不能在此上下文中使用。

我正在使用 Visual Studio 2010,.NET 4.0 中的 WPF 应用程序。

I am having a problem in the xml serialization of observable collection.

Here is what I am serializing:

public enum Status { Pending, Active, Completed, Cancelled }

public abstract class Entity : INotifyPropertyChanged
{
    ...
}

public class UserStory : Entity
{
    public uint StoryID { get; set; }
    public Status Status { get; set; }
    ...
    public ObservableCollection<Task> Tasks { get; set; }
}

public class Task : Entity
{
    public uint TaskID { get; set; }
    ...
}

Here is how I serialize it:

public static void SerializeObjectToXML<T>(T item, string FilePath)
{
    XmlSerializer xs = new XmlSerializer(typeof(T));
    using (StreamWriter wr = new StreamWriter(FilePath))
    {
        xs.Serialize(wr, item);
    }
}

public class Main()
{
    ObservableCollection<UserStory> UserStories { get; set; }

    void Main()
    {
        ...
        ObservableCollection<object> Document = new ObservableCollection<object>();
        Document.Add(UserStories);
        SerializeObjectToXML<ObservableCollection<object>>(Document , "...");
        ...
    }
}

But an error occur in the xs.Serialize(wr, item); line saying:

InvalidOperation Exception: There was an error generating the XML document.
Inner Exception: The type ScrumPresentor.ObservableCollection`1[[ScrumPresentor.UserStory,
ScrumPresentor, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]] may not be used in this context.

I am using Visual Studio 2010, WPF application in .NET 4.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2024-12-29 21:05:10

尝试使用 System.Xml.Serialization.XmlInclude 属性。我不确定我是否正确理解了您收集集合的意图,但假设这就是您想要的,这是一个可行的解决方案。输出如下代码:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;

namespace StackoverflowXxmlSerialize
{

    public enum Status { Pending, Active, Completed, Cancelled }

    [System.Xml.Serialization.XmlInclude(typeof(UserStory))]
    [System.Xml.Serialization.XmlInclude(typeof(Task))]    
    public abstract class Entity : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }


    public class UserStory : Entity
    {
        public uint StoryID { get; set; }
        public Status Status { get; set; }
        public ObservableCollection<Task> Tasks { get; set; }
    }

    public class Task : Entity 
    {
        public uint TaskID { get; set; }
    }

    class Util
    {
        public static void SerializeObjectToXML<T>(T item, string FilePath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            using (StreamWriter wr = new StreamWriter(FilePath))
            {
                xs.Serialize(wr, item);
            }
        }
    }



    public class TestSerialize
    {
        static ObservableCollection<Entity> UserStories { get; set; }

        public static void RunTest()
        {
            UserStories = new ObservableCollection<Entity> { 
                new UserStory  {
                StoryID = 127,
                Status = Status.Active,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 11 }, new Task { TaskID = 12 }}
                },

                new UserStory  {
                StoryID = 128,
                Status = Status.Cancelled,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 13 }, new Task { TaskID = 14 }}
                },

                new UserStory  {
                StoryID = 129,
                Status = Status.Completed,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 9 }, new Task { TaskID = 10 }}
                },
            };


            ObservableCollection<ObservableCollection<Entity>> Document
                = new ObservableCollection<ObservableCollection<Entity>>();

            Document.Add(UserStories);
            Util.SerializeObjectToXML<ObservableCollection<ObservableCollection<Entity>>>(Document, @"d:\tmp\junk.txt");

        }
    }

}

上面的代码生成了这个文本文件:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArrayOfEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ArrayOfEntity>
    <Entity xsi:type="UserStory">
      <StoryID>127</StoryID>
      <Status>Active</Status>
      <Tasks>
        <Task>
          <TaskID>11</TaskID>
        </Task>
        <Task>
          <TaskID>12</TaskID>
        </Task>
      </Tasks>
    </Entity>
    <Entity xsi:type="UserStory">
      <StoryID>128</StoryID>
      <Status>Cancelled</Status>
      <Tasks>
        <Task>
          <TaskID>13</TaskID>
        </Task>
        <Task>
          <TaskID>14</TaskID>
        </Task>
      </Tasks>
    </Entity>
    <Entity xsi:type="UserStory">
      <StoryID>129</StoryID>
      <Status>Completed</Status>
      <Tasks>
        <Task>
          <TaskID>9</TaskID>
        </Task>
        <Task>
          <TaskID>10</TaskID>
        </Task>
      </Tasks>
    </Entity>
  </ArrayOfEntity>
</ArrayOfArrayOfEntity>

Try using the System.Xml.Serialization.XmlInclude attribute. I'm not sure if I correctly understand your intent of a collection of collections, but assuming that is what you want, here is a working solution. The output follows the code:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;

namespace StackoverflowXxmlSerialize
{

    public enum Status { Pending, Active, Completed, Cancelled }

    [System.Xml.Serialization.XmlInclude(typeof(UserStory))]
    [System.Xml.Serialization.XmlInclude(typeof(Task))]    
    public abstract class Entity : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }


    public class UserStory : Entity
    {
        public uint StoryID { get; set; }
        public Status Status { get; set; }
        public ObservableCollection<Task> Tasks { get; set; }
    }

    public class Task : Entity 
    {
        public uint TaskID { get; set; }
    }

    class Util
    {
        public static void SerializeObjectToXML<T>(T item, string FilePath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            using (StreamWriter wr = new StreamWriter(FilePath))
            {
                xs.Serialize(wr, item);
            }
        }
    }



    public class TestSerialize
    {
        static ObservableCollection<Entity> UserStories { get; set; }

        public static void RunTest()
        {
            UserStories = new ObservableCollection<Entity> { 
                new UserStory  {
                StoryID = 127,
                Status = Status.Active,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 11 }, new Task { TaskID = 12 }}
                },

                new UserStory  {
                StoryID = 128,
                Status = Status.Cancelled,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 13 }, new Task { TaskID = 14 }}
                },

                new UserStory  {
                StoryID = 129,
                Status = Status.Completed,
                Tasks = new ObservableCollection<Task>{new Task { TaskID = 9 }, new Task { TaskID = 10 }}
                },
            };


            ObservableCollection<ObservableCollection<Entity>> Document
                = new ObservableCollection<ObservableCollection<Entity>>();

            Document.Add(UserStories);
            Util.SerializeObjectToXML<ObservableCollection<ObservableCollection<Entity>>>(Document, @"d:\tmp\junk.txt");

        }
    }

}

The above code produced this text file:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArrayOfEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ArrayOfEntity>
    <Entity xsi:type="UserStory">
      <StoryID>127</StoryID>
      <Status>Active</Status>
      <Tasks>
        <Task>
          <TaskID>11</TaskID>
        </Task>
        <Task>
          <TaskID>12</TaskID>
        </Task>
      </Tasks>
    </Entity>
    <Entity xsi:type="UserStory">
      <StoryID>128</StoryID>
      <Status>Cancelled</Status>
      <Tasks>
        <Task>
          <TaskID>13</TaskID>
        </Task>
        <Task>
          <TaskID>14</TaskID>
        </Task>
      </Tasks>
    </Entity>
    <Entity xsi:type="UserStory">
      <StoryID>129</StoryID>
      <Status>Completed</Status>
      <Tasks>
        <Task>
          <TaskID>9</TaskID>
        </Task>
        <Task>
          <TaskID>10</TaskID>
        </Task>
      </Tasks>
    </Entity>
  </ArrayOfEntity>
</ArrayOfArrayOfEntity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文