简体   繁体   English

对象未将所有属性序列化为XML

[英]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. State属性的类型为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. USStateCodesType包含带有字符串键和值的私有字典。 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. USStatesCodeType上方的属性与前面的示例相同。

The problem is, when I try to serialize the object to an XML String, i get something like this: 问题是,当我尝试将对象序列化为XML String时,我得到如下信息:

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

In my Database, I am storing CA. 在我的数据库中,我正在存储CA。 I want the XML to put out 我想把XML放出来

  <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的实例并指定了各种属性:

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. 如果忘记了该选项,则“ State字段将不会显示在生成的序列化XML中。

Set that boolean property, and it will show up! 设置该布尔属性,它将显示出来!

要执行所需的操作,您需要实现ISerializable接口的GetObjectData方法,并实现一个受保护的序列化构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM