【问题标题】:XML serialization reference - duplicateXML 序列化参考 - 重复
【发布时间】:2014-12-27 23:31:43
【问题描述】:

[Java 或 C#] 我在序列化方面遇到了一些问题。如何不复制有关对象的所有信息并仅使用参考?

示例类:

class Author {
  public String id;
  public String name;
}

class Book {
  public String id;
  public Author author;
  public String title;
}

我必须像这里一样格式化输出文件:

<store>
  <authors>
   <author id="PK">
     <name>Philip Kindred</name>
    </author>
  </authors>

  <books>
    <book id="u1">
      <author>PK</author> <!--  use only ID -->
      <title>Ubik</title>
    </book>
  </books>
</store>

【问题讨论】:

  • 您能解释一下“复制有关对象的所有信息”的意思吗?您的意思是“如何避免为我的类中的每个字段手动手动编码 XML 输出”?如果是这样,在 c# 中,您可以使用 XmlSerializer 使用反射自动序列化到 XML 和从 XML 反序列化。
  • 这不是问题。在书籍声明中,我不想每次都复制作者对象,而是使用对单个对象的引用(例如通过作者 ID)。
  • 手头的问题显然是如何在 XML 中保持聚合关系。鉴于 XML 的分层特性(非常适合表达组合但不是聚合)总是需要额外的逻辑才能使其工作,请参阅下面的一个可能的答案。

标签: java c# xml serialization


【解决方案1】:

您面临在 XML 中表示聚合而不是组合关系的问题。当您保持父拥有子关系的父子关系(即组合)时,XML 序列化非常简单。在这种情况下,一本书有一个(或多个)作者,但并不拥有它,因为一个作者可以是许多其他书籍的作者。

在这种情况下,您可以做类似于数据库中所做的事情,即有两个单独的条目并通过外键表达关系。请参阅下面的示例:

[Serializable]
public class Author
{
    [XmlAttribute("id")]
    public String Id { get; set; }
    [XmlElement("name")]
    public String Name { get; set; }
}

[Serializable]
public class Book
{
    private Author _author;

    [XmlIgnore]
    public Author Author
    {
        get { return _author; }
        set
        {
            _author = value;
            AuthorId = _author != null ? _author.Id : null;
        }
    }

    [XmlAttribute("id")]
    public String Id { get; set; }

    [XmlElement("author")]
    public String AuthorId { get; set; }

     [XmlElement("title")]
    public String Title { get; set; }
}

[Serializable]
public class Store
{
    [XmlArray("authors")]
    [XmlArrayItem("author", Type = typeof(Author))]
    public List<Author> Authors { get; set; }

    [XmlArray("books")]
    [XmlArrayItem("book", Type = typeof(Book))]
    public List<Book> Books { get; set; }

    public Store()
    {
        Books = new List<Book>();
        Authors = new List<Author>();
    }
}


internal class Program
{
    private static void Main(string[] args)
    {
        // Create some authors
        var authors = new List<Author>
        {
            new Author{Id="PK", Name="Philip Kindred"},
            new Author{Id="WS", Name="William Shakespeare"},
        };

        // Create some books linked to the authors
        var books = new List<Book>
        {
            new Book{Id = "U1", Author = authors[0], Title = "Do Androids Dream of Electric Sheep?"},
            new Book{Id = "U2", Author = authors[1], Title = "Romeo and Juliet"}
        };

        var store = new Store {Authors = authors, Books = books};

        var success = Serialiser.SerialiseToXml(store, "store.xml");

        // Deserialize the data from XML
        var store2 = Serialiser.DeserialseFromXml<Store>("store.xml");

        // Resolve the actual Author instances from the saved IDs (foreign key equivalent in databases)
        foreach (var book in store2.Books)
            book.Author = store2.Authors.FirstOrDefault(author => author.Id == book.AuthorId);

        // Now variable 'store' and 'store2' have the same equivalent data
    }
}

// Helper class to serialize and deserialize the data to XML file
public static class Serialiser
{
    public static bool SerialiseToXml(object obj, string filename)
    {
        try
        {
            var ws = new XmlWriterSettings
            {
                NewLineHandling = NewLineHandling.Entitize,
                NewLineChars = Environment.NewLine,
                Indent = true,
                NewLineOnAttributes = false
            };
            var xs = new XmlSerializer(obj.GetType());
            using (var writer = XmlWriter.Create(filename, ws))
                xs.Serialize(writer, obj);

            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
    }

    public static T DeserialseFromXml<T>(string filename) where T : new()
    {
        var typeofT = typeof(T);
        try
        {
            var xs = new XmlSerializer(typeofT);
            using (var reader = XmlReader.Create(filename))
                return (T)xs.Deserialize(reader);
        }
        catch(Exception ex)
        {
            return default(T);
        }
    }
}

“store.xml”将如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Store xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<authors>
    <author id="PK">
    <name>Philip Kindred</name>
    </author>
    <author id="WS">
    <name>William Shakespeare</name>
    </author>
</authors>
<books>
    <book id="U1">
    <author>PK</author>
    <title>Do Androids Dream of Electric Sheep?</title>
    </book>
    <book id="U2">
    <author>WS</author>
    <title>Romeo and Juliet</title>
    </book>
</books>
</Store>

【讨论】:

    【解决方案2】:

    c# 答案

    如果你有一些更大的容器类可以管理对象之间的交叉引用,你可以这样做。在您的情况下,您似乎有一个可以用于此目的的 Store 对象。 Store 按姓名维护书籍和作者的字典; Book 记住其作者的id,并根据需要从存储中获取实际的Author。当然,这需要AuthorBook 都知道它们所在的商店。

    一个示例实现可能如下:

    public class Author
    {
        string id;
        Store store;
    
        [XmlIgnore]
        public Store Store {
            get {
                return store;
            }
            set {
                if (store != null && id != null)
                    store.Authors.Remove(id);
                this.store = value;
                if (store != null && id != null)
                    store.Authors[id] = this;
            }
        }
    
        [XmlAttribute("id")]
        public string Id
        {
            get
            {
                return id;
            }
            set
            {
                if (store != null && id != null)
                    store.Authors.Remove(id);
                this.id = value;
                if (store != null && id != null)
                    store.Authors[id] = this;
            }
        }
    
        [XmlElement("name")]
        public string Name { get; set; }
    }
    
    public class Book
    {
        string authorId;
        string id;
        Store store;
    
        [XmlIgnore]
        public Store Store
        {
            get
            {
                return store;
            }
            set
            {
                if (store != null && id != null)
                    store.Books.Remove(id);
                this.store = value;
                if (store != null && id != null)
                    store.Books[id] = this;
            }
        }
    
        [XmlAttribute("id")]
        public string Id
        {
            get
            {
                return id;
            }
            set
            {
                if (store != null && id != null)
                    store.Books.Remove(id);
                this.id = value;
                if (store != null && id != null)
                    store.Books[id] = this;
            }
        }
    
        [XmlElement("author")]
        public string AuthorID
        {
            get
            {
                return authorId;
            }
            set
            {
                authorId = value;
            }
        }
    
        [XmlIgnore]
        public Author Author
        {
            get
            {
                if (store == null)
                    return null;
                if (AuthorID == null)
                    return null;
                return store.Authors[AuthorID];
            }
            set
            {
                if (value == Author)
                    return;
                if (value == null)
                {
                    authorId = null;
                }
                else
                {
                    if (value.Id == null)
                        throw new ArgumentException();
                    authorId = value.Id;
                }
    
                AssertCorrectAuthor(value);
            }
        }
    
        [Conditional("DEBUG")]
        private void AssertCorrectAuthor(Author author)
        {
            if (store != null)
                Debug.Assert(author == Author);
        }
    
        [XmlElement("title")]
        public string Title { get; set; }
    }
    
    [XmlRoot("store")]
    public class Store
    {
        readonly Dictionary<string, Book> books = new Dictionary<string, Book>();
        readonly Dictionary<string, Author> authors = new Dictionary<string, Author>();
    
        [XmlIgnore]
        public IDictionary<string, Book> Books
        {
            get
            {
                return books;
            }
        }
    
        [XmlIgnore]
        public IDictionary<string, Author> Authors
        {
            get
            {
                return authors;
            }
        }
    
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [XmlArray("authors")]
        [XmlArrayItem("author")]
        public Author[] AuthorList // proxy array for serialization.
        {
            get
            {
                return Authors.Values.ToArray();
            }
            set
            {
                foreach (var author in authors.Values)
                {
                    author.Store = null;
                }
                Authors.Clear();
                if (value == null)
                    return;
                foreach (var author in value)
                {
                    author.Store = this;
                }
            }
        }
    
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        [XmlArray("books")]
        [XmlArrayItem("book")]
        public Book[] BookList // proxy array for serialization.
        {
            get
            {
                return Books.Values.ToArray();
            }
            set
            {
                foreach (var book in Books.Values)
                {
                    book.Store = null;
                }
                Books.Clear();
                if (value == null)
                    return;
                foreach (var book in value)
                {
                    book.Store = this;
                }
            }
        }
    }
    

    并且,测试:

    public static class TestStore
    {
        public static void Test()
        {
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <store>
      <authors>
       <author id=""PK"">
         <name>Philip Kindred</name>
        </author>
      </authors>
    
      <books>
        <book id=""u1"">
          <author>PK</author> <!--  use only ID -->
          <title>Ubik</title>
        </book>
        <book id=""t1"">
          <author>PK</author> <!--  use only ID -->
          <title>The Transmigration of Timothy Archer</title>
        </book>
      </books>
    </store>
    ";
            var store = xml.LoadFromXML<Store>();
            Debug.Assert(store.BookList[0].Author == store.AuthorList[0]); // no assert
            Debug.Assert(store.BookList[1].Author == store.AuthorList[0]); // no assert; verify that all books use the same instance of the `Author` class.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 2013-04-28
      相关资源
      最近更新 更多