反序列化xml时,属性返回Null(它具有值) [英] Attribute returns Null ( it is having value) when deserializating an xml
问题描述
我在对xml字符串进行序列化和反序列化时遇到问题.
我的函数将xml作为字符串获取,并且应该返回和对象.现有的代码是
Hi,
I am facing an issue while serialization and deserialzation of an xml string.
My function gets xml as a string and it should return and object. the existing code is
MemoryStream oMemoryStream = null;
object oRootData = null;
XmlSerializer oXmlSerializer = new XmlSerializer(oType);
UTF8Encoding oUTF8Encoding = new UTF8Encoding();
Byte[] Bytes = oUTF8Encoding.GetByte(sXml);
oMemoryStream = new MemoryStream(Bytes);
oRootData = oXmlSerializer.Deserialize(oMemoryStream);
直到我在XSD文件中添加新字段(sector_id)之前,它都工作正常.现在,sXml包含根据XSD的新属性和值,但是当它反序列化时,属性ector_id的值将变为0.
It was working fine till the time i have added a new field (sector_id) in my XSD file. Now the sXml contains the new attribute and value as per the XSD but when it deseialize the value for attribute sector_id becomes 0.
Can any one of please help me out of this ?
推荐答案
听起来像潜在的问题是进行序列化的代码不知道新属性.它们都在同一个项目中吗?
Sounds like the potential issue is that the code doing the serialising does not know about the new attribute. Are they all in the same project ?
我使用的是
[System.Xml.Serialization.XmlIgnoreAttribute()]
而不是
[System.Xml.Serialization.XmlAttributeAttribute()]
,所以编译器在运行时忽略了特定属性以进行序列化.
so the particular attribute was ignored by complier at run time for serialization.
这篇关于反序列化xml时,属性返回Null(它具有值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!