English 中文(简体)
XmlSerializer doesn t serialize everything in my class
原标题:

I have a very basic class that is a list of sub-classes, plus some summary data.

[Serializable]
public class ProductCollection : List<Product>
{
    public bool flag { get; set; }
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }
}


// method to save this class
private void SaveProductCollection()
{
    // Export case as XML...
    XmlSerializer xml = new XmlSerializer(typeof(ProductCollection));
    StreamWriter sw = new StreamWriter("output.xml");
    xml.Serialize(sw, theCollection);
    sw.Close();
}

When I call SaveProductCollection() I get the following:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Product>
    <InputType>1</InputType>
  </Product>
  <Product>
    <InputType>1</InputType>
  </Product>
</ArrayOfProduct>

Note that I have the base type : List<Product>. But I don t have any of the class properties: flag, A, B, C.

Did I do something wrong? What s up??

UPDATE Thanks for the replies. I wasn t aware that it was by-design. I ve converted to BinaryFormatter (for binary serialization instead) and it works wonderfully.

最佳回答

Following msdn:

Q: Why aren t all properties of collection classes serialized?

A: The XmlSerializer only serializes the elements in the collection when it detects either the IEnumerable or the ICollection interface. This behavior is by design. The only work around is to re-factor the custom collection into two classes, one of which exposes the properties including one of the pure collection types.

问题回答

First of all, when making your own collection types, you should inherit System.Collections.ObjectModel.Collection<T>, which will allow you to override InsertItem and other methods to gain complete control over the collection.

To answer your question, XmlSerializer handles collection types differently to serialize the items in the collection. It will not serialize any properties (eg, List<T>.Capacity).

You could move your properties to a different type that would contain the properties or the collection.

You could also try using the attributes that control XML serialization, but I m not sure whether they ll help.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签