Deserialize httpWebResponse into object - c#

I received the web response and need to desterilize into the list. I get an error "Root element is missing". Would someone tell me how to solve it. Thanks.
I debug the code and get the response text:
<ArrayOfLocation xmlns="http://schemas.datacontract.org/2004/07/Ordinging.Objects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Location>
<locationID>401</locationID>
<locationName>Burnaby</locationName>
</Location>
<Location>
<locationID>101</locationID>
<locationName>Vancouver</locationName>
</Location>
</ArrayOfLocation>
My code to desterilize:
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
XmlSerializer serializer = new XmlSerializer(typeof(List<LocationList.Location>));
List<LocationList.Location> data = new List<LocationList.Location>();
data = serializer.Deserialize(reader) as List<LocationList.Location>;
}
The Location Class in my app:
public class LocationList
{
private List<Location> locations = null;
[XmlElement("loctions")]
public List<Location> locs
{
get { return locations; }
set { locations = value; }
}
public class Location
{
public string locationName { get; set; }
public Int64 locationID { get; set; }
public Location(string name, Int64 id)
{
locationID = id;
locationName = name;
}
public Location() { }
}
}

One way to do this. Change it to use the xml from your response instead. I used a hardcoded string just for my testing).
Edit : Added helper function to ignore namespace if you need to do that. Otherwise xml should match namespace.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace TestCodeApp {
class TestCode {
static void Main () {
string xmlString = #"
<ArrayOfLocation xmlns='http://schemas.datacontract.org/2004/07/Ordinging.Objects' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<Location>
<locationID>401</locationID>
<locationName>Burnaby</locationName>
</Location>
<Location>
<locationID>101</locationID>
<locationName>Vancouver</locationName>
</Location>
</ArrayOfLocation>";
StringReader stringReader = new StringReader (xmlString);
XmlSerializer serializer = new XmlSerializer (typeof (List<Location>), new XmlRootAttribute ("ArrayOfLocation"));
List<Location> locations = (List<Location>) serializer.Deserialize (new XmlTextReaderHelper(stringReader));
foreach (Location item in locations) Console.WriteLine (item);
}
}
public class XmlTextReaderHelper : XmlTextReader {
public XmlTextReaderHelper (System.IO.TextReader reader) : base (reader) { }
public override string NamespaceURI {
get { return ""; }
}
}
public class Location {
public int locationID { get; set; }
public string locationName { get; set; }
public override string ToString () {
return "ID: " + locationID + " - " + locationName;
}
}
}

Related

Deserialize xml into class having string values in xml

Let me explain, there is a database table which has 1 XML column named audits and other common types of column.
so is this possible to deserialize below XML into class.
<?xml version="1.0"?>
<entity type="Order">
<id type="System.Int64">146</id>
<ordernumber type="System.String">OD555</ordernumber>
<audits type='System.String'>
<audit>
<item>
<create timestamp='2017-07-19 10:02:13' userid='23' />
</item>
<invoice>
<create timestamp='2017-07-19 10:03:37' userid='45' />
</invoice>
</audit>
</audits>
</entity>
Class:
public class Order
{
public long id { get; set; }
public string ordernumber { get; set; }
public string audits { get; set; }
}
Modifying your model with the attributes XmlType and XmlAnyElement (requires XmlElement as type)
[XmlType("entity")]
public class Order
{
public long id { get; set; }
public string ordernumber { get; set; }
[XmlAnyElement]
public XmlElement audits { get; set; }
}
allows to deserialize the complete XML string like
using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(xmlString);
writer.Flush();
stream.Position = 0;
XmlSerializer serializer = new XmlSerializer(typeof(Order));
Order o = (Order)serializer.Deserialize(stream);
}
Now you are able to get the audits as string like
string auditsString = o.audits.InnerXml;
You can also add a property to your model to simplify the access:
public string auditsString
{
get
{
return audits.InnerXml;
}
}
Try xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication68
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Order order = doc.Descendants("entity").Select(x => new Order()
{
id = (long)x.Element("id"),
ordernumber = (string)x.Element("ordernumber"),
audits = x.Descendants("create").Select(y => (DateTime)y.Attribute("timestamp")).ToList()
}).FirstOrDefault();
}
}
public class Order
{
public long id { get; set; }
public string ordernumber { get; set; }
public List<DateTime> audits { get; set; }
}
}
You can try like this
const string xmlString = #"<columns><column><c1>100</c1><c2>200</c2><cn>300</cn></column><column><c1>111</c1><c2>222</c2><cn>333</cn></column> <column> <c1>MAX Newsletter</c1><c2>OLS Application</c2> <cn>Total funded accounts</cn> </column></columns>";
XDocument doc = XDocument.Parse(xmlString);
if (doc.Root != null)
{
List<Row> items = (from r in doc.Root.Elements("column")
select new Row
{
C1 = (string)r.Element ("C1"),
C2 = (string)r.Element("C2"),
}).ToList();

LINQ2XML approach to generalize iteration through xml

I am trying to parse some xml files (using LINQ) to use that data to store in DB. Following is the format for one of the service, and there are dozens of them more, each with different node patterns and structure.
Is there a way to generalize this approach, so that I can iterate all these services through a single method/lines of code? without being bothered by how the childNodes are arranged ?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<deviceState>
<id>203948-2908093-203984902-29348020-290lk</id>
<device>Mirron</device>
<variable>
<id>Plug - Meter.VI</id>
<text>
<textValue>
<value>0.000000</value>
</textValue>
</text>
</variable>
<variable>
<id>AEB</id>
<text>
<textStr>-</textStr>
</text>
</variable>
<variable>
<id>DESCRIPTION</id>
<text>
<textStr />
</text>
</variable>
<variable>
<id>VDTTM</id>
<text>
<textDate>
<date>01042016103658</date>
</textDate>
</text>
</variable>
<variable>
<id>STATUS</id>
<text>
<textValue>
<value>1.000000</value>
</textValue>
</text>
</variable>
</deviceState>
I want to achieve a functionality, where I can access values of every variable id by specifying a search filter and then access it's value directly, without being bothered by following meaningless tags.
<text><textValue> or <text><textDate> or <text><textStr/> or <text>
<textvalue><value>
something like, lets say new devices {a.id=node("id"), a.value=node("valu")}.
Currently I am using following code which does the trick but its not efficient, neither in speed, nor in manageability.
XDocument x = XDocument.Parse(_back);
string back = "";
string xvalue, yvalue;
foreach (XElement xe in x.Descendants().Take(1))
{
var s = xe.Value.IndexOf("Plug - Meter.VI");
var val = xe.Value.Substring(s, 25);
back= val.Substring(val.LastIndexOf("I")+1, 10);
break;
}
Any guidance will be highly appreciated. Thanks
Based on Monty's feedback, this is what I am using.
public protoDCM_CornerL08UMS_View()
{
InitializeComponent();
synchronizationContext = SynchronizationContext.Current;
StartingMeter();
}
private async void StartingMeter()
{
string val="0";
max = min = 0;
await Task.Run(() =>
{
do
{
UpdateUI(val,max,min);
val = fetchData();
Double temp =0;
if (Double.TryParse(val,out temp))
{
if(min==0&&max==0)
{
min = max = temp;
}
if(temp>max)
{
max = temp;
}
if(temp<min)
{
min = temp;
}
}
val = temp.ToString();
}
while (true);
});
}
private void UpdateUI(string value, Double _max , Double _min)
{
var timeNow = DateTime.Now;
if ((DateTime.Now - previousTime).Milliseconds <= 50) return;
synchronizationContext.Post(new SendOrPostCallback(o =>
{
lblInstant.Text= (string)o;
}), value);
synchronizationContext.Post(new SendOrPostCallback(o =>
{
lblMax.Text = (string)o;
}), _max.ToString());
synchronizationContext.Post(new SendOrPostCallback(o =>
{
lblMin.Text = (string)o;
}), _min.ToString());
previousTime = timeNow;
}
public Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
private string fetchData()
{
String _back = HttpGet("http://xxx.xxx.xxx"+Global.Node.Current.Device.Address+"/services/devices/deviceState.xml?id=D83AE139-E0C9-4B15-B2A9-6E0B57B28ED1?type=ALL");
//_back = FormatXML(Response);
try
{
DeviceState deserializedXML = new DeviceState();
XmlSerializer serializer = new XmlSerializer(typeof(DeviceState));
using (Stream stream = GenerateStreamFromString(_back))
{
deserializedXML = (DeviceState)serializer.Deserialize(stream);
var x = (from z in deserializedXML.Variable
where z.Id == "Plug - Meter.VI"
select new
{
Id = z.Id,
value= (z.Text.TextDate == null?
(z.Text.TextStr == null
? (z.Text.TextValue == null
? "No Text Value!" : z.Text.TextValue.Value.ToString()) : z.Text.TextStr.ToString()) : z.Text.TextDate.Date.ToString()) }).FirstOrDefault();
return x.value;
}
}
catch (Exception w)
{
MessageBox.Show(w.ToString());
return "0.0";
}
}
public static string HttpGet(string URI)
{
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Method = "GET";
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
catch (Exception sl)
{
return sl.ToString();
}
}
Try this....
Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
Classes
[XmlRoot(ElementName = "textValue")]
public class TextValue
{
[XmlElement(ElementName = "value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "text")]
public class Text
{
[XmlElement(ElementName = "textValue")]
public TextValue TextValue { get; set; }
[XmlElement(ElementName = "textStr")]
public string TextStr { get; set; }
[XmlElement(ElementName = "textDate")]
public TextDate TextDate { get; set; }
}
[XmlRoot(ElementName = "variable")]
public class Variable
{
[XmlElement(ElementName = "id")]
public string Id { get; set; }
[XmlElement(ElementName = "text")]
public Text Text { get; set; }
}
[XmlRoot(ElementName = "textDate")]
public class TextDate
{
[XmlElement(ElementName = "date")]
public string Date { get; set; }
}
[XmlRoot(ElementName = "deviceState")]
public class DeviceState
{
[XmlElement(ElementName = "id")]
public string Id { get; set; }
[XmlElement(ElementName = "device")]
public string Device { get; set; }
[XmlElement(ElementName = "variable")]
public List<Variable> Variable { get; set; }
}
code
try
{
DeviceState deserializedXML = new DeviceState();
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(DeviceState));
using (FileStream stream = File.OpenRead(#"xml.xml"))
{
deserializedXML = (DeviceState)serializer.Deserialize(stream);
// Now get all your IDs
List<string> IDs = (from xml in deserializedXML.Variable select xml.Id).ToList();
} // Put a break-point here, then mouse-over IDs and you will see all your IDs... deserializedXML contains the entire object if you want anythin else ....
}
catch (Exception)
{
throw;
}
I read your XML from a file (xml.xml) that is in the application *.exe folder, you will need to adapt this solution depending on your specific requirements....
Is this what you need?....
try
{
XmlSerializer DeserializerPlaces = new XmlSerializer(typeof(DeviceState));
string html = string.Empty;
string url = #"https://<Your URL>";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
DeviceState dezerializedXML = (DeviceState)DeserializerPlaces.Deserialize(reader);
//html = reader.ReadToEnd();
}
//Console.WriteLine(html);
}
catch (System.Exception)
{
throw;
}
You would call that every 5 seconds and update your UI (from dezerializedXML properties)

Problems deserializing List of objects

I am having trouble deserializing a list of objects. I can get just one object to serialize into an object but cannot get the list. I get no error it just returns an empty List. This is the XML that gets returned:
<locations>
<location locationtype="building" locationtypeid="1">
<id>1</id>
<name>Building Name</name>
<description>Description of Building</description>
</location>
</locations>
This is the class I have and I am deserializing in the GetAll method:
[Serializable()]
[XmlRoot("location")]
public class Building
{
private string method;
[XmlElement("id")]
public int LocationID { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("mubuildingid")]
public string MUBuildingID { get; set; }
public List<Building> GetAll()
{
var listBuildings = new List<Building>();
var building = new Building();
var request = WebRequest.Create(method) as HttpWebRequest;
var response = request.GetResponse() as HttpWebResponse;
var streamReader = new StreamReader(response.GetResponseStream());
TextReader reader = streamReader;
var serializer = new XmlSerializer(typeof(List<Building>),
new XmlRootAttribute() { ElementName = "locations" });
listBuildings = (List<Building>)serializer.Deserialize(reader);
return listBuildings;
}
}
Try this:
[XmlRoot("locations")]
public class BuildingList
{
public BuildingList() {Items = new List<Building>();}
[XmlElement("location")]
public List<Building> Items {get;set;}
}
Then deserialize the whole BuildingList object.
var xmlSerializer = new XmlSerializer(typeof(BuildingList));
var list = (BuildingList)xmlSerializer.Deserialize(xml);
I know this is an old(er) question, but I struggled with this today, and found an answer that doesn't require encapsulation.
Assumption 1: You have control over the source Xml and how it is constructed.
Assumption 2: You are trying to serialise the Xml directly into a List<T> object
You must name the Root element in the Xml as ArrayOfxxx where xxx is the name of your class (or the name specified in XmlType (see 2.))
If you wish your xml Elements to have a different name to the class, you should use XmlType on the class.
NB: If your Type name (or class name) starts with a lowercase letter, you should convert the first character to uppercase.
Example 1 - Without XmlType
class Program
{
static void Main(string[] args)
{
//String containing the xml array of items.
string xml =
#"<ArrayOfItem>
<Item>
<Name>John Doe</Name>
</Item>
<Item>
<Name>Martha Stewart</Name>
</Item>
</ArrayOfItem>";
List<Item> items = null;
using (var mem = new MemoryStream(Encoding.Default.GetBytes(xml)))
using (var stream = new StreamReader(mem))
{
var ser = new XmlSerializer(typeof(List<Item>)); //Deserialising to List<Item>
items = (List<Item>)ser.Deserialize(stream);
}
if (items != null)
{
items.ForEach(I => Console.WriteLine(I.Name));
}
else
Console.WriteLine("No Items Deserialised");
}
}
public class Item
{
public string Name { get; set; }
}
Example 2 - With XmlType
class Program
{
static void Main(string[] args)
{
//String containing the xml array of items.
//Note the Array Name, and the Title case on stq.
string xml =
#"<ArrayOfStq>
<stq>
<Name>John Doe</Name>
</stq>
<stq>
<Name>Martha Stewart</Name>
</stq>
</ArrayOfStq>";
List<Item> items = null;
using (var mem = new MemoryStream(Encoding.Default.GetBytes(xml)))
using (var stream = new StreamReader(mem))
{
var ser = new XmlSerializer(typeof(List<Item>)); //Deserialising to List<Item>
items = (List<Item>)ser.Deserialize(stream);
}
if (items != null)
{
items.ForEach(I => Console.WriteLine(I.Name));
}
else
Console.WriteLine("No Items Deserialised");
}
}
[XmlType("stq")]
public class Item
{
public string Name { get; set; }
}
Not sure how Building corresponds with the location you have in your xml, but to me it makes more sense if they're named equivalently. Instead of using a List use a LocationList, and it becomes:
[Serializable()]
[XmlRoot("locations")]
public class LocationCollection{
[XmlElement("location")]
public Location[] Locations {get;set;}
}
[Serializable()]
[XmlRoot("location")]
public class Location
{
[XmlElement("id")]
public int LocationID { get; set; }
[XmlAttribute("locationtype")]
public string LocationType {get;set;}
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("mubuildingid")]
public string MUBuildingID { get; set; }
}
You can then deserialize as follows:
var request = WebRequest.Create(method) as HttpWebRequest;
var response = request.GetResponse() as HttpWebResponse;
var streamReader = new StreamReader(response.GetResponseStream());
TextReader reader = streamReader;
var serializer = new XmlSerializer(typeof(LocationCollection),
new XmlRootAttribute() { ElementName = "locations" });
var listBuildings = (LocationCollection)serializer.Deserialize(reader);
return listBuildings;
I know, old question, but came across it when faced by a similar issue.
Building on #ricovox's answer and in context of the OP's question, this is the model I would use to serialize his xml:
[Serializable, XmlRoot("locations")]
public class BuildingList
{
[XmlArrayItem("location", typeof(Building))]
public List<Building> locations { get; set; }
}
[Serializable]
public class Building
{
public int LocationID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MUBuildingID { get; set; }
public List<Building> GetAll()
{
...
}
}
The OP's mistake was to create the list item as the root
Use [XMLArray] for collection properties.

There is an error in XML document (1,2) , System.InvalidOperationException: <AuthorizationResult xlms:""> was not expected

XML sent from API
<AuthenticationResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<PAPIErrorCode>0</PAPIErrorCode>
<ErrorMessage/>
<AccessToken>StringAccessToken</AccessToken>
<AccessSecret>StringAccessToken</AccessSecret>
<PolarisUserID>PolarisSampleUser</PolarisUserID>
<BranchID>7</BranchID>
<AuthExpDate>2013-05-27T16:57:46.323</AuthExpDate>
</AuthenticationResult>
Classes for Response
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace PAPIAutomatedTestingTool
{
[XmlRoot(ElementName="AuthorizationResult")]
public class AuthorizationResult
{
public int PAPIErrorCode { get; set; }
public string ErrorMessage { get; set; }
public string AccessToken { get; set; }
public string AccessSecret { get; set; }
public int PolarisUserID { get; set; }
public int BranchID { get; set; }
public DateTime AuthExpDate { get; set; }
}
}
Code making request and deserializing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Security.Cryptography;
using System.Xml;
using System.Xml.Serialization;
namespace PAPIAutomatedTestingTool
{
public class APICallMethods
{
public string URI { get; set; }
public string accSecret { get; set; }
public string accToken { get; set; }
public string authorizationString { get; set; }
public bool AuthenticateStaffUser()
{
try
{
//Initializing all variables
string authReqMethod = "POST";
string authAccessKey = "Sample Access Key";
string authAccessKeyID = "Sample Access ID";
string authPatronPassword = "";
DateTime authDateTime = DateTime.Now;
string httpAuthDateTime = authDateTime.ToUniversalTime().ToString("r");
string authURI = "Sample URI";
//Composing the papiHash from the given parameters
string papiHash = GetPAPIHash(authAccessKey, authReqMethod, authURI, httpAuthDateTime, authPatronPassword);
//Formating the authorization string
string authorizationString = String.Format("Authorization: PWS {0}:{1}", authAccessKeyID, papiHash);
//Creating and defining the WebRequest
WebRequest req = WebRequest.Create(authURI);
req.Method = "POST";
req.Headers.Add("PolarisDate", httpAuthDateTime);
req.Headers.Add(authorizationString);
req.ContentType = "application/xml";
string requestBody = "<AuthenticationData><Domain>SampleDomain</Domain><Username>SampleUsername</Username><Password>SamplePassword</Password></AuthenticationData>";
byte[] reqBodyBytes = System.Text.Encoding.UTF8.GetBytes(requestBody);
req.ContentLength = reqBodyBytes.Length;
using (Stream requestStream = req.GetRequestStream())
{
requestStream.Write(reqBodyBytes, 0, reqBodyBytes.Length);
}
//Receiving the WebResponse
using (WebResponse resp = req.GetResponse())
{
AuthorizationResult firstResponse = new AuthorizationResult();
Stream respStream = resp.GetResponseStream();
StreamReader sr = new StreamReader(respStream);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(AuthorizationResult));
firstResponse = (AuthorizationResult)xmlSerializer.Deserialize(respStream);
Console.WriteLine("Authorization: PWS" + firstResponse.AccessSecret + ":" + firstResponse.AccessToken);
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
return false;
}
}
public string GetPAPIHash(string strAccessKey, string strHTTPMethod, string strURI, string strHTTPDate, string strPatronPassword)
{
byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(strAccessKey);
HMACSHA1 hmac = new HMACSHA1(secretBytes);
byte[] dataBytes = null;
if (strPatronPassword.Length > 0)
{
dataBytes = UTF8Encoding.UTF8.GetBytes(strHTTPMethod + strURI + strHTTPDate + strPatronPassword);
}
else
{
dataBytes = UTF8Encoding.UTF8.GetBytes(strHTTPMethod + strURI + strHTTPDate);
}
byte[] computedHash = hmac.ComputeHash(dataBytes);
string computedHashString = Convert.ToBase64String(computedHash);
return computedHashString;
}
}
}
I am making a POST request to the API with a body that contains AuthorizationData. The API is supposed to return the xml to deserialize into firstresponse. I have received the xml(confirmed by printing to console) but i am receiving the There is an error in the XML Document(1,2) and <AuthorizationData xmlns=""> was not expected. Thanks for the help in advance.
It appears to be complaining about an unexpected root element and there is some confusion surrounding that elsewhere in your question.
In the question title you have <AuthorizationResult>
In the example XML response you have <AuthenticationResult>
In the C# you have [XmlRoot(ElementName="AuthorizationResult")]
At the end of your question you mention <AuthorizationData>
That's quite a few subtle variations on a theme.
Assuming the XML sample response is correct, you should change the C# to expect the root element to be <AuthenticationResult>...
[XmlRoot(ElementName="AuthenticationResult")]
public class AuthenticationResult
{
...
If you want to write a single deserialize for both schema and non-schema custom XMLs, you can turn off namespaces to avoid getting the 'There is an error in the XML Document(1,2)' error. But since the XmlReader expects a XML Schema, you should read it with StringReader class then use XmlReader object to remove custom namespaces.
//Namespaces
using System.IO;
using System.Xml.Serialization;
//Field
private static XmlSurrogates.Root _xmlSurrogates = new();
//Method
private static void DeserializeXml(string xml)
{
try
{
XmlSerializer serializer = new XmlSerializer(_xmlSurrogates.GetType());
using (StringReader reader = new StringReader(xml))
{
var xmlTextReader = new System.Xml.XmlTextReader(reader);
xmlTextReader.Namespaces = false;
_xmlSurrogates = (XmlSurrogates.Root)(serializer.Deserialize(xmlTextReader));
}
}
catch (Exception ex)
{
Log.Error($"There is an error while deserialize. Error: {ex.Message}");
}
}
//Root Model Example
public class XmlSurrogates
{
[XmlRoot(ElementName = "XMLROOTELEMENTNAMEHERE FOR THIS ITS root")]
public class Root
{
[XmlElement(ElementName = "Boo")] public Boo Boo { get; set; }
[XmlElement(ElementName = "Control")] public ClassName DeclareName { get; set; }
[XmlAttribute(AttributeName = "MainCode")] public string MainCode { get; set; }
[XmlAttribute(AttributeName = "Caption")] public string Caption { get; set; }
}
[XmlRoot(ElementName = "Boo")]
public class ElementAsClassName
{
[XmlAttribute(AttributeName = "Foo")] public string PropertyName { get; set; }
}
}

Parse XML file and populate object with values

After I send a request with the required parameters in the response I get the following XML:
<content>
<main>
<IMGURL>image url</IMGURL>
<IMGTEXT>Click Here</IMGTEXT>
<TITLE>image title</TITLE>
<IMGLINK>image link</IMGLINK>
</main>
</content>
and I also made the following two classes:
[Serializable]
public class content
{
private Main _main;
public content()
{
_main = new Main();
}
public Main Main
{
get { return _main; }
set { _main = value; }
}
}
[Serializable]
public class Main
{
public string IMGURL { get; set; }
public string IMGTEXT { get; set; }
public string TITLE { get; set; }
public string IMGLINK { get; set; }
}
While debugging I can see that in the response I get the wanted results. However I'm having troubles deserializing the XML and populating the object.
Call to the method:
public static class ImageDetails
{
private static string _url = ConfigurationManager.AppSettings["GetImageUrl"];
public static content GetImageDetails(string ua)
{
var contenta = new content();
_url += "&ua=" + ua;
try
{
WebRequest req = WebRequest.Create(_url);
var resp = req.GetResponse();
var stream = resp.GetResponseStream();
//var streamreader = new StreamReader(stream);
//var content = streamreader.ReadToEnd();
var xs = new XmlSerializer(typeof(content));
if (stream != null)
{
contenta = (content)xs.Deserialize(stream);
return contenta;
}
}
catch (Exception ex)
{
}
return new content();
}
}
The serializer is case-sensitive. You either need to rename the property content.Main to main or add the attribute [XmlElement("main")] to it.

Categories