XmlSerializer 将数组属性 isFixedSize 设置为 true
我正在使用 XmlSerializer 将某些 XML 反序列化为对象。我遇到的问题是,一旦我成功反序列化,我的一个属性(数组)的属性 isFixedSize
设置为 true。
我需要在稍后阶段手动添加到该数组,但不能。
这是我的对象(为简洁起见,省略了其他属性)
namespace Omeda.Customer
{
[Serializable()]
[XmlRoot("Customer")]
public class Customer : Error
{
[XmlArray("CustomerDemographics")]
[XmlArrayItem("CustomerDemographic", typeof(CustomerDemographic))]
public Omeda.Customer.CustomerDemographic[] CustomerDemographics { get; set; }
}
}
这是我用来反序列化的方法(为简洁起见,再次省略了代码)
private T request_Get<T>(string url) where T : new()
{
object returnObject = new T();
try
{
var request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/xml";
request.Headers.Add("x-omeda-appid", this.API_KEY);
request.Timeout = 50000;
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
returnObject = (T)serializer.Deserialize(responseStream);
}
}
}
catch (WebException ex)
{
...
}
return (T)returnObject;
}
返回此对象后,customer.CustomerDemographics.IsFixedSize
返回 true。
任何关于如何解决这个问题以及为什么会发生这种情况的帮助都会很棒。
I'm using an XmlSerializer to deserialize some XML into an object. The problem i'm having is once i've deserialized successfully, one of my properties which is an array has the property isFixedSize
set to true.
I need to add to this array manually at a later stage, but can't.
Here's my object (other properties omitted for brevity)
namespace Omeda.Customer
{
[Serializable()]
[XmlRoot("Customer")]
public class Customer : Error
{
[XmlArray("CustomerDemographics")]
[XmlArrayItem("CustomerDemographic", typeof(CustomerDemographic))]
public Omeda.Customer.CustomerDemographic[] CustomerDemographics { get; set; }
}
}
And here's the method I'm using to deserialize (again, code ommitted for brevity)
private T request_Get<T>(string url) where T : new()
{
object returnObject = new T();
try
{
var request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/xml";
request.Headers.Add("x-omeda-appid", this.API_KEY);
request.Timeout = 50000;
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
returnObject = (T)serializer.Deserialize(responseStream);
}
}
}
catch (WebException ex)
{
...
}
return (T)returnObject;
}
Once this object is returned, customer.CustomerDemographics.IsFixedSize
returns true.
Any help on how to get round this, and why this is happening would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IsFixedSize
对于数组始终为 true。如果您“需要在稍后阶段手动添加到此数组”,则不应使用数组;您可能应该使用List
或其他可以增长的集合类型。IsFixedSize
is always true for an array. If you "need to add to this array manually at a later stage", you should not be using an array; you should probably be using aList<CustomerDemographic>
, or another collection type that can be grown.您是否尝试将这些 CustomerDemographic 节点序列化为集合?
为此,请将
typeof(CustomerDemographic)
更改为typeof(List)
。然后您就可以更改该集合中的项目。或者:
Did you try serializing those CustomerDemographic nodes to a collection instead?
To do that, change
typeof(CustomerDemographic)
totypeof(List<CustomerDemographic>)
.then you'll be able to change items in that collection. Alternatively: