Hi..
I Have A Simple API that contain XML response That I want to populate the data with global function
[XmlRoot(ElementName = "ArrayOfWell", Namespace = "http://www.w3.org/2001/XMLSchema")]
public class ArrayOfWell
{
[XmlElement("Well")]
public List<WellResponse> WellResponseXML { get; set; }
}
public class WellResponse
{
public string UWI { get; set; }
public string? AREA_TYPE { get; set; }
}
controller :
public async Task<JsonResult> DataWellXML()
{
string baseAddress = _configuration["ApiCredential:BaseAddress"];
string userName = _configuration["ApiCredential:UserName"];
string passWord = _configuration["ApiCredential:Password"];
string endPoint = "getwell-xml";
string apiUrl = baseAddress + userName + "," + passWord + "/" + endPoint;
var data = await _apiService.GetXMLAsync<ArrayOfWell>(apiUrl);
return Json(data);
}
Service:
public async Task<T> GetXMLAsync <T> (string url)
{
try
{
// Send an HTTP GET request to the URL
HttpResponseMessage response = await _httpClient.GetAsync(url);
// Ensure the response is successful
response.EnsureSuccessStatusCode();
// Read the content as a string (XML content)
string xmlContent = await response.Content.ReadAsStringAsync();
// Deserialize the XML into the generic type T
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xmlContent))
{
return (T)serializer.Deserialize(reader);
}
}
catch (Exception ex)
{
throw new InvalidOperationException("Failed to retrieve or deserialize XML.", ex);
}
}
Sample xml API response :
<?xml version="1.0"?>
<ArrayOfWell xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Well>
<UWI>CDNPR-01</UWI>
<AREA_TYPE>Block</AREA_TYPE>
</Well>
<Well>
<UWI>CDNPR-02</UWI>
<AREA_TYPE>Block</AREA_TYPE>
</Well>
</ArrayOfWell>
But when the service try to Deserialize the xml Content , I Got Error like this:
ErrorMessage:
"<ArrayOfWell xmlns=''> was not expected."
I hope somebody can help me. Thanks