简体   繁体   中英

How to deserialize XML having prefix in C#

Here is my XML

<?xml version="1.0" encoding="UTF-8"?>
 <ls:LeadResponse xmlns:ls="example.org/ls">
    <ls:status>OK</ls:status>
    <ls:code>approved</ls:code>
    <ls:message>posted</ls:message>
 </ls:LeadResponse>

It has the prefix ls everywhere.

I have done like this for deserialization

Model created:

 [XmlRoot(ElementName = "LeadResponse",Namespace="example.org/ls")]
 public class LeadProviderResponse
 {
     [XmlElement(Namespace = "example.org/ls")]
     public string Status { get; set; }

     [XmlElement(Namespace = "example.org/ls")]
     public string Code { get; set; }

     [XmlElement(Namespace = "example.org/ls")]
     public string Message { get; set; }
 }

and the code for deserialization

using var streamReader = new StringReader(xmlContent); 
using var xmlReader = XmlReader.Create(streamReader, null);    
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(LeadProviderResponse));
var result = (LeadProviderRespone)serializer.Deserialize(xmlReader);

It is not able to deserialize correctly. Can anyone tell me where I am doing wrong?

Casing:

[XmlElement(Namespace = "example.org/ls", ElementName = "status")]
public string Status { get; set; }

[XmlElement(Namespace = "example.org/ls", ElementName = "code")]
public string Code { get; set; }

[XmlElement(Namespace = "example.org/ls", ElementName = "message")]
public string Message { get; set; }

or (same result):

[XmlElement("status", Namespace = "example.org/ls")]
public string Status { get; set; }

[XmlElement("code", Namespace = "example.org/ls")]
public string Code { get; set; }

[XmlElement("message", Namespace = "example.org/ls")]
public string Message { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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