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>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!
要执行所需的操作,您需要实现ISerializable
接口的GetObjectData
方法,并实现一个受保护的序列化构造函数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.