C# GET REST xml to object deserialization encoding clash - c#

I wanna get xml file from http and convert it to object.
So right now I have 2 methods: one to get http response body like that:
var httpClient = new HttpClient();
var op = httpClient.GetStringAsync(uri);
var httpResponseBody = "";
try {
var httpResponse = await httpClient.GetAsync(uri);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
return httpResponseBody;
}
...
which returns string httpResponseBody.
Second one tries to convert this xml in string to object:
res = await task;
var reader = new XmlSerializer(typeof(Schedule));
using (var tr = new MemoryStream(Encoding.UTF8.GetBytes(res)))
{
var schedule = (Schedule)reader.Deserialize(tr);
return schedule;
}
The problem is that the content I receive is in different encoding and I don't know how to convert it to make deserialization possible.
I am getting something like this:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ramowka><dzien name=\"PoniedziaÅ\u0082ek\" count=\"2\"/></ramowka>\n
How to get rid of '\n' and Å\u0082 (should be ł) ?
Right now I am getting Exception from reader.Deserialize: {"<ramowka xmlns=''> was not expected."}
Schedule class:
[XmlType(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Schedule
{
[XmlElementAttribute("ramowka")]
public ScheduleDay[] AuditionDays { get; set; }
}

I've changed Schedule class to:
[XmlType(AnonymousType = true)]
[XmlRootAttribute("ramowka")]
public class Schedule
{
[XmlElementAttribute("dzien")]
public ScheduleDay[] AuditionDays { get; set; }
}
Now it looks like working. Thanks Petter for hint with Root attribute.

Setting the root object on the XmlSerializer fixes the problem:
var reader = new XmlSerializer(typeof(Schedule), new XmlRootAttribute("ramowka"));
...though I used slightly different attributes:
[DataContract]
public class ScheduleDay
{
[DataMember, XmlAttribute]
public string name { get; set; }
[DataMember, XmlAttribute]
public string count { get; set; }
}
[DataContract]
public class Schedule
{
[DataMember]
public ScheduleDay dzien { get; set; }
}
I haven't tried yours yet, but these work.
For a collection of ScheduleDays, this combo works:
[XmlType("dzien")]
public class ScheduleDay
{
[XmlAttribute]
public string name { get; set; }
[XmlAttribute]
public string count { get; set; }
}
Usage:
XmlSerializer reader = new XmlSerializer(typeof(List<ScheduleDay>), new XmlRootAttribute("ramowka"));
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(Xml)))
{
List<ScheduleDay> schedule = (List<ScheduleDay>)reader.Deserialize(stream);
}
The Schedule class just disappeared from the equation.
Escapes in the HTML
The \ns are part of the XML structure, so no need to worry about those. The deserializer will translate \u0082 into its equivalent character, which is
BREAK PERMITTED HERE. Which you probably don't want. The Å looks out of place too -- it's the last letter of the Norwegian alphabet and not used in Polish, AFAIK.

Related

ReadAsAsync Class using XmlMediaTypeFormatter or other Deserialization

I am trying to deserialize the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo
xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<id>asjdkfljasl;kdf</id>
<operation>query</operation>
<object>jsdkfjsakldjakl</object>
...
</jobInfo>
I have the following code that makes the POST request and works successfully, but cannot deserialize into my class.
client.DefaultRequestHeaders.Add("X-SFDC-Session", binding.SessionHeaderValue.sessionId);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", binding.SessionHeaderValue.sessionId);
var content = new StringContent(createJobXml, Encoding.UTF8, "application/xml");
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
HttpResponseMessage response = await client.PostAsync(
$"https://{SERVER_INSTANCE}.salesforce.com/services/async/43.0/job", content
);
response.EnsureSuccessStatusCode();
jobInfo job = await response.Content.ReadAsAsync<jobInfo >(new List<MediaTypeFormatter>() {
new XmlMediaTypeFormatter { UseXmlSerializer = true },
new JsonMediaTypeFormatter()
});
But I get the error
No MediaTypeFormatter is available to read an object of type 'jobInfo ' from content with media type 'application/xml',
my jobInfo was generated using xsd.exe doc.xml, xsd.exe doc.xsd /classes
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.force.com/2009/06/asyncapi/dataload")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.force.com/2009/06/asyncapi/dataload", IsNullable = false)]
public partial class jobInfo
{
private string idField;
private string operationField;
private string objectField;
...
public string id
{
get {
return this.idField;
}
set {
this.idField = value;
}
}
public string operation
{
get {
return this.operationField;
}
set {
this.operationField = value;
}
}
...
}
What am I missing in order to deserialize this correctly?
This suggests that I should just read it as a string:
How to use HttpClient to read an XML response?
but this suggests that it should "just work"
HttpClient ReadAsAsync<Type> only deserializing part of the response
I have also tried (was using class Bulkv1Job before I used xsd to convert the xml to a class)
[DataContract]
public class Bulkv1Job
{
[DataMember]
string id { get; set; }
[DataMember]
string operation { get; set; }
[DataMember]
string #object { get; set; }
...
}
and
[XmlRoot("jobInfo")]
public class Bulkv1Job
{
[XmlElement("id")]
string id { get; set; }
[XmlElement("operation")]
string operation { get; set; }
...
}
After testing with
var s = await response.content.readasstringasync();
var buffer = encoding.utf8.getbytes(s);
using (var stream = new memorystream(buffer)) {
var serializer = new xmlserializer(typeof(jobinfo)); // FAILED LINE
var jobinfo = (jobinfo)serializer.deserialize(stream);
//then do whatever you want
return jobinfo;
}
I found that the serializer failed on the above line due to a class protection error.
Ensuring the jobInfo class was public and accessible, the error went away and the deserialize(stream) worked.
Removing that code and using readAsAsync<jobInfo>(...) worked appropriately afterwards.
Thanks #Ryan
Hmmm I'm not an expert with soap but it should have decorators like [DataMember] on your properties and [DataContract(NameSpace="jobInfo")] on your class if I remember well.
You can see that in the first suggestion you provide the guy use [XmlRoot] and [XmlElement]
You can also see WCF Datacontract, some fields do not deserialize , maybe it would help you

Issue with Deserialization with JSON with C#

I have looked over example after example after example and none of my attempts have worked.
I'm attempting to deserialize this JSON return:
{
"status": "success",
"data": {
"result": "match",
"id_user": 26564,
"dob_match": null,
"first_name_match": null,
"last_name_match": null
},
"code": 200
}
Here is my JSON object class declaration:
[DataContract]
internal class DccCoachApi
{
[DataMember]
public string result { get; set; }
public string id_user { get; set; }
public string dob_match { get; set; }
public string first_name_match { get; set; }
public string last_name_match { get; set; }
}
In my stream method, my streamRead variable is filled with:
{"status":"success","data":{"result":"match","id_user":26564,"dob_match":null,"first_name_match":null,"last_name_match":null},"code":200}
Method 1 does not populate coachId:
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(streamRead)))
{
// Deserialization from JSON
var deserializer = new DataContractJsonSerializer(typeof(DccCoachApi));
var dccObj = (DccCoachApi)deserializer.ReadObject(ms);
coachId = dccObj.id_user;
}
Nor does method 2:
DccCoachApi coach = new JavaScriptSerializer().Deserialize<DccCoachApi>(streamRead);
coachId = coach.id_user;
nor does method 3:
JavaScriptSerializer js = new JavaScriptSerializer();
DccCoachApi dccObj = js.Deserialize<DccCoachApi>(streamRead);
coachId = dccObj.id_user;
nor does method 4:
dynamic dccObject = js.Deserialize<dynamic>(streamRead);
coachId = dccObject["id_user"];
The hard error that gets produced when i pull the value directly off method 4 is:
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
Methods 1-3 do not hit a hard error, however they populate coachId with no data.
Can somebody please let me know what i'm doing wrong?
You can simply generate proper classes here: http://json2csharp.com/
This is how it should look like you don't need the DataMember Attributes, it might confuse the serializer to only de-serialize this single property:
public class Data
{
public string result { get; set; }
public int id_user { get; set; }
public object dob_match { get; set; }
public object first_name_match { get; set; }
public object last_name_match { get; set; }
}
public class RootObject
{
public string status { get; set; }
public Data data { get; set; }
public int code { get; set; }
}
Code:
var deserializer = DataContractJsonSerializer(typeof(RootObject));
var root = (RootObject)deserializer.ReadObject(ms);
var coachId = root.data.id_user;
I revised the code to look like this, and it is dumping my value perfectly. Thanks much to everyone who helped me reach the solution. Plutonix, thanks as well for the paste special 411. I had no idea that existed. VERY useful!
using (var reader = new StreamReader(webResponse.GetResponseStream()))
{
var streamRead = reader.ReadToEnd();
reader.Close();
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(streamRead)))
{
JavaScriptSerializer js = new JavaScriptSerializer();
DccCoachRootobject dccObj = js.Deserialize<DccCoachRootobject>(streamRead);
coachId = dccObj.data.id_user.ToString();
}
}

Issue de-serializing XML to Object - There is an error in XML document (0, 0)

I'm trying to read in element values from a simple XML doc and bind these to an object however I'm running into problems with my XML document. I have validated it and can confirm there are no issues with the document itself however expanding the results on the line:
var nodes = from xDoc in xml.Descendants("RewriteRule")
select xmlSerializer.Deserialize(xml.CreateReader()) as Url;
Show "There is an error in XML document (0, 0)"
The inner exceptions reads <RewriteRules xmlns=''> was not expected.
I'm not sure what I'm doing wrong here?
My XML is below:
<?xml version="1.0" encoding="utf-8" ?>
<RewriteRules>
<RewriteRule>
<From>fromurl</From>
<To>tourl</To>
<Type>301</Type>
</RewriteRule>
</RewriteRules>
The code that loads the XML file and attempts to de-serialize it: -
public static UrlCollection GetRewriteXML(string fileName)
{
XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
var xmlSerializer = new XmlSerializer(typeof(Url));
var nodes = from xDoc in xml.Descendants("RewriteRule")
select xmlSerializer.Deserialize(xml.CreateReader()) as Url;
return nodes as UrlCollection;
}
My Url object class: -
[Serializable]
[XmlRoot("RewriteRule")]
public class Url
{
[XmlElement("From")]
public string From { get; set; }
[XmlElement("To")]
public string To { get; set; }
[XmlElement("Type")]
public string StatusCode { get; set; }
public Url()
{
}
public Url(Url url)
{
url.From = this.From;
url.To = this.To;
url.StatusCode = this.StatusCode;
}
}
Can anyone see what I'm doing wrong here?
Thanks
I'm not too familiar with the from select statement, but it seems you just pass in xml which is the whole XDocument, instead of the XElement that is your RewriteRule. That is why you get the error message that RewriteRules is unknown - the XmlSerializer expects a single RewriteRule.
I managed to rewrite your code using LINQ instead (but if you know how to get the single element from the from select statement, that should work equally well).
This should give you the correct result - rr is the XElement that is returned from Descendants:
public static IEnumerable<Url> GetRewriteXML()
{
XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
var xmlSerializer = new XmlSerializer(typeof(Url));
var nodes = xml.Descendants("RewriteRule")
.Select(rr => xmlSerializer.Deserialize(rr.CreateReader()) as Url);
return nodes;
}
EDIT:
The name of your Url class does not match. You need to rename it to "RewriteRule"or define it this way:
[Serializable]
[System.Xml.Serialization.XmlRoot("RewriteRule")]
public class Url
{
[XmlElement("From")]
public string From { get; set; }
[XmlElement("To")]
public string To { get; set; }
[XmlElement("Type")]
public string StatusCode { get; set; }
public Url()
{
}
public Url(Url url)
{
url.From = this.From;
url.To = this.To;
url.StatusCode = this.StatusCode;
}
}
my approach is more used, when you deserialize it directly to an instance of a class.
If you want to use XDocument you may just write it like this. I do not use XDocument in my code. Since I need to deserialize the full xml packages I do it directly with deserialize of the root node. Therefore, I have proposed to the the root node to the right place in the previous post.
With XDocument you can access subparts directly. Here is working code for your purpose, but there may be others who can help you setting this code up more elegant:
public static UrlCollection GetRewriteXML(string fileName)
{
XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
var urls = from s in xml.Descendants("RewriteRule")
select new
{
From = (string)s.Element("From").Value,
To = (string)s.Element("To").Value,
StatusCode = (string)s.Element("Type").Value
};
UrlCollection nodes = new UrlCollection();
foreach (var url in urls)
{
nodes.Add(new Url(url.From, url.To, url.StatusCode));
}
return nodes;
}
[Serializable]
public class Url
{
[XmlElement("From")]
public string From { get; set; }
[XmlElement("To")]
public string To { get; set; }
[XmlElement("Type")]
public string StatusCode { get; set; }
public Url()
{
}
public Url(string From, string To, string StatusCode)
{
this.From = From;
this.To = To;
this.StatusCode = StatusCode;
}
public Url(Url url)
{
url.From = this.From;
url.To = this.To;
url.StatusCode = this.StatusCode;
}
}

Metro App - deserialize JSON String

I'd like to deserialize a JSON string which I get from a webservice. My problem is, that the deserialized object class array (of type Result) has always 0 items in it....
But the webservice returns the correct string.
So I think the failure occurs in the way how I deserialize the string/stream.
Any ideas what's my fault?
//JSON result string:
{"Results":
[{"Result":{
"Name":"Rechnung2",
"Date1":"2012-10-05",
"Item1":"50",
"Item2":"10",
"CompanyName":"Contoso",
"Description":"My description"}}]
}
[DataContract]
public class Result
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Date1 { get; set; }
[DataMember]
public string Item1 { get; set; }
[DataMember]
public string Item2 { get; set; }
[DataMember]
public string CompanyName { get; set; }
[DataMember]
public string Description { get; set; }
}
public async void GetjsonStream()
{
HttpClient client = new HttpClient();
string url = "http://localhost/test/api.php?format=json&key=12345";
HttpResponseMessage response = await client.GetAsync(url);
//ReadAsStringAsync() works fine, so I think ReadAsStreamAsync() works also fine
var str = await response.Content.ReadAsStreamAsync();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Result[]));
//Result has always 0 items
Result[] res = (Result[])ser.ReadObject(str);
}
I haven't used DataContractJsonSerializer myself, so this may not be the best approach - but I suspect that the problem is that the JSON represents "an object containing a collection of results" - not "a collection of results".
Try this, in addition to your existing code:
[DataContract]
public class ResultCollection
{
[DataMember]
public Result[] Results { get; set; }
}
...
var ser = new DataContractJsonSerializer(typeof(ResultCollection));
var collection = (ResultCollection)ser.ReadObject(str);
var results = collection.Results;
You may be able to change the type of Results to List<Result> too, if that's helpful.
(I've just tried the code above, and it gave me the right result, so it looks like this is at least along the right lines...)

Missing JSON field causes crashes?

My Code goes like this:
using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
{
string resultString = streamReader1.ReadToEnd();
var ser = new DataContractJsonSerializer(typeof(RootObject));
var stream = new MemoryStream(Encoding.Unicode.GetBytes(resultString));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
RootObject myBook = (RootObject)jsonSerializer.ReadObject(stream);
Deployment.Current.Dispatcher.BeginInvoke(() => Shops.ItemsSource = myBook.SearchResponse.Spell.Results); }
public class Query
{
public string SearchTerms { get; set; }
}
public class Result
{ [DataMember(IsRequired=false)]
public string Value { get; set; }
}
public class Spell
{
[DataMember(IsRequired = false)]
public int Total { get; set; }
[DataMember(IsRequired = false)]
public List<Result> Results { get; set; }
}
public class SearchResponse
{
public bool IsRequired { get; set; }
public string Version { get; set; }
public Query Query { get; set; }
public Spell Spell { get; set; }
}
public class RootObject
{
public SearchResponse SearchResponse { get; set; }
}
IF JSON DATA EXISTS
{"SearchResponse":{"Version":"2.0","Query":{"SearchTerms":"mispell"},"Spell":{"Total":1,"Results":[{"Value":"misspell"}]}}}
IF JSON DATA DOES NOT EXIST
{"SearchResponse":{"Version":"2.0","Query":{"SearchTerms":"mispel"}}}
The thing is, if Bing doesn't detect a wrong word, it crashes and gives me an error like NullReferenceException. I have tried to do an IF statement looking at the stream for if it's value is blank but doesn't seem to work.
Any ideas?
If you receive a JSON answer without the Spell part, the Spell property in SearchResponse will be null. If it's null, you may not dereference it like this:
myBook.SearchResponse.Spell.Results
(This hasn't anything to do with JSON. It's how C# works.)
So instead of:
Shops.ItemsSource = myBook.SearchResponse.Spell.Results
you probably want to write:
if (myBook.SearchResponse.Spell = null)
Shops.ItemsSource = myBook.SearchResponse.Spell.Results;
else
Shops.ItemsSource = new List<Result>();
For your next question: It would be very helpful if your questions would show the exact error message including the stack trace (or at least the exact line where it occurred).
Use [DataMember(IsRequired=false)], MSDN: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.isrequired.aspx

Categories