【发布时间】:2011-05-19 20:05:29
【问题描述】:
我有一个接受标准地址属性并存储它们的类。 State 属性的类型为 USStateCodesType。以下是用于存储属性的代码示例:
[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;
}
}
USStateCodesType 包含一个带有字符串键和值的私有字典。默认构造函数加载字典并由任何重载调用。只有一个公共财产,国家。编码如下:
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;
}
}
USStatesCodeType 上方的属性与前面的示例相同。
问题是,当我尝试将对象序列化为 XML 字符串时,我得到如下内容:
<BusinessAddress>
<Address1>12345 AnyStreet</Address1>
<City>Los Angles</City>
<Zip>90210</Zip>
</BusinessAddress>
在我的数据库中,我存储 CA。我希望 XML 输出
<BusinessAddress>
<Address1>12345 AnyStreet</Address1>
<City>Los Angles</City>
<State>California</State>
<Zip>90210</Zip>
</BusinessAddress>
我在序列化之前检查了对象的属性,并且 State Property 将 California 显示为值。
我做错了什么?
【问题讨论】:
标签: c# xml-serialization child-objects