开发者

Object not serializing all properties to XML

I have a class that takes standard address properties and stores them. The State property is of type USStateCodesType. Here's a sample of the code used to store the properties:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://SP/Items/Schemas")]
public partial class BusinessAddress
{

    private string address1Field;

    private string address2Field;

    private string cityField;

    private USStateCodesType stateField;

    private bool stateFieldSpecified;

    private string zipField;

    /// <remarks/>
    public string Address1
    {
        get
        {
            return this.address1Field;
        }
        set
        {
            this.address1Field = value;
        }
    }

The USStateCodesType contains a private dictionary with a string key and value. The default constructor loads up the dictionary and is called by any overloads. There is only one public Property, State. It is coded as follows:

    public string State
    {
        get
        {
            return iDict[_key];
        }
        set
        {
            if (iDict.ContainsValue(value))
            {
                foreach (string k in iDict.Keys)
                    if (iDict[k] == value)
                        _key = k;
            }
            else
                _key = string.Empty;                    
        }
    }

The attributes above the USStatesCodeType are identical to the earlier example.

The problem is, when I try to serialize the object to an XML String, i get something like this:

  <BusinessAddress>
    <Address1>12345 AnyStreet</Address1>
    <City>Los Angles</City>
    <Zip>90210</Zip>
  </BusinessAddress>

In my Database, I am storing CA. I want the XML to put out

  <BusinessAddress>
    <Address1>12345 AnyStreet</Address1>
    <City>Los Angles</City>
    <State开发者_如何学JAVA>California</State>
    <Zip>90210</Zip>
  </BusinessAddress>

I check the properties of the object prior to serialization and the State Property shows California as the value.

What am I doing wrong?


I would assume that you created an instance of BusinessAddress and specified the various properties:

BusinessAddress myBusinessAddress = new BusinessAddress();
myBusinessAddress.Address1 = "12345 AnyStreet";
myBusinessAddress.City = "Los Angeles";
myBusinessAddress.Zip = 90210;
myBusinessAddress.State = "California";

but most likely, you didn't specify:

myBusinessAddress.StateFieldSpecified = true;

If you forget that option, your State field will not show up in the resulting serialized XML.

Set that boolean property, and it will show up!


To do what you want you need to implement the GetObjectData method of the ISerializable interface, and implement the a protected serialization constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜