[英]How to deserialize xml to object in C#, If some xml missing in the xml
I had one xml payload, there are lot of xml elements available, I have declared all the elements in the class properties, when I tried to serialize it came with all nodes, Here I have removed some xml elements before deserialize, then am trying to deserialize the xml, here am getting error. 我有一个xml有效负载,有很多可用的xml元素,我已经声明了类属性中的所有元素,当我尝试序列化时,它带有所有节点,这里我在反序列化之前删除了一些xml元素,然后我试图反序列化xml,这里得到错误。
Is that possible to deserialize without declared elements? 没有声明的元素可以反序列化吗? I need to deserialize without some elements.
我需要在没有某些元素的情况下反序列化。 Because deserialize xml payload is dynamic, it is coming from another vendor.
因为反序列化xml有效负载是动态的,所以它来自另一个供应商。 Please help me.
请帮我。
Code Block: 代码块:
Class: 类:
[Serializable]
public class CSSngSectorCable
{
[XmlElement("cableID")]
public Int32 CSSngCableId { get; set; }
public string RFDSSection { get; set; }
public string RFDSSector { get; set; }
public Int32 RFDSCableNumber { get; set; }
[XmlElement("cableNumber")]
public string CSSngCableNumber { get; set; }
[XmlElement("feederType")]
public string CSSngFeederType { get; set; }
[XmlElement("feederLength")]
public string CSSngFeederLength { get; set; }
public int CableRowNumber { get; set; }
}
Code to serialize: 要序列化的代码:
var cables= new SectorCable();
///added some code
string serializevalue = SerializationHelper.SerializeToXml(cables, true);
Serialize Output: 序列化输出:
<cable>
<cableID>1</cableID>
<cableNumber>2</cableNumber>
<feederType>15.24</feederType>
<feederLength></feederLength>
</cable>
Data to deserialize: 要反序列化的数据:
<cable>
<cableID>1</cableID>
<cableNumber>2</cableNumber>
</cable>
Code for deserialize: 反序列化代码:
public CSSngSectorCable GetCableFromDynamicXML(StreamReader reader)
{
try
{
var serializer = new XmlSerializer(typeof(CSSngSectorCable));
var cables = (CSSngSectorCable)serializer.Deserialize(reader);
return cables;
}
catch (Exception ex)
{
throw;
}
Here it's not working fine it's throw exception. 在这里它没有正常工作它抛出异常。 How to resolve the issue.
如何解决问题。
public class MyClass
{
[XmlElement(IsNullable = true)]
public string Group;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.