Deserializing XML from String
I'm trying to convert the result I get from my web service as a string and convert it to an object.
This is the string I'm getting from my service:
<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>
So I have a class for this as:
[XmlRoot]
public class StatusDocumentItem
{
[XmlElement]
public string DataUrl;
[XmlElement]
public string LastUpdated;
[XmlElement]
public string Message;
[XmlElement]
public int State;
[XmlElement]
public string StateName;
}
And this is how I'm trying to get that string as an object of type StatusDocumentItem with XMLDeserializer (NB. operationXML contains the string):
string operationXML = webRequest.getJSON(args[1], args[2], pollURL);
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(operationXML))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
But my result object is always empty. What am I doing wrong?
Update. The value I get from my operationXML is like this and has an unnecessary xmlns attribute that is blocking my deserialization. Without that attribute, everything is working fine. Here is how it looks like:
"<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>"
c# .net xml xml-deserialization
|
show 8 more comments
I'm trying to convert the result I get from my web service as a string and convert it to an object.
This is the string I'm getting from my service:
<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>
So I have a class for this as:
[XmlRoot]
public class StatusDocumentItem
{
[XmlElement]
public string DataUrl;
[XmlElement]
public string LastUpdated;
[XmlElement]
public string Message;
[XmlElement]
public int State;
[XmlElement]
public string StateName;
}
And this is how I'm trying to get that string as an object of type StatusDocumentItem with XMLDeserializer (NB. operationXML contains the string):
string operationXML = webRequest.getJSON(args[1], args[2], pollURL);
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(operationXML))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
But my result object is always empty. What am I doing wrong?
Update. The value I get from my operationXML is like this and has an unnecessary xmlns attribute that is blocking my deserialization. Without that attribute, everything is working fine. Here is how it looks like:
"<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>"
c# .net xml xml-deserialization
1
"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.
– Christian.K
Feb 1 '13 at 12:10
1
If you set your xml example to operationXML. The deserialization works perfectly well.
– Cédric Bignon
Feb 1 '13 at 12:12
Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"
– disasterkid
Feb 1 '13 at 12:14
1
@Pedram string operationXML = "<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>";
– Cédric Bignon
Feb 1 '13 at 12:16
1
@Pedram I have result.Message = "Job put in queue".
– Cédric Bignon
Feb 1 '13 at 12:43
|
show 8 more comments
I'm trying to convert the result I get from my web service as a string and convert it to an object.
This is the string I'm getting from my service:
<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>
So I have a class for this as:
[XmlRoot]
public class StatusDocumentItem
{
[XmlElement]
public string DataUrl;
[XmlElement]
public string LastUpdated;
[XmlElement]
public string Message;
[XmlElement]
public int State;
[XmlElement]
public string StateName;
}
And this is how I'm trying to get that string as an object of type StatusDocumentItem with XMLDeserializer (NB. operationXML contains the string):
string operationXML = webRequest.getJSON(args[1], args[2], pollURL);
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(operationXML))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
But my result object is always empty. What am I doing wrong?
Update. The value I get from my operationXML is like this and has an unnecessary xmlns attribute that is blocking my deserialization. Without that attribute, everything is working fine. Here is how it looks like:
"<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>"
c# .net xml xml-deserialization
I'm trying to convert the result I get from my web service as a string and convert it to an object.
This is the string I'm getting from my service:
<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>
So I have a class for this as:
[XmlRoot]
public class StatusDocumentItem
{
[XmlElement]
public string DataUrl;
[XmlElement]
public string LastUpdated;
[XmlElement]
public string Message;
[XmlElement]
public int State;
[XmlElement]
public string StateName;
}
And this is how I'm trying to get that string as an object of type StatusDocumentItem with XMLDeserializer (NB. operationXML contains the string):
string operationXML = webRequest.getJSON(args[1], args[2], pollURL);
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(operationXML))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
But my result object is always empty. What am I doing wrong?
Update. The value I get from my operationXML is like this and has an unnecessary xmlns attribute that is blocking my deserialization. Without that attribute, everything is working fine. Here is how it looks like:
"<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>"
c# .net xml xml-deserialization
c# .net xml xml-deserialization
edited Feb 1 '13 at 12:36
disasterkid
asked Feb 1 '13 at 12:08
disasterkiddisasterkid
2,3781453106
2,3781453106
1
"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.
– Christian.K
Feb 1 '13 at 12:10
1
If you set your xml example to operationXML. The deserialization works perfectly well.
– Cédric Bignon
Feb 1 '13 at 12:12
Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"
– disasterkid
Feb 1 '13 at 12:14
1
@Pedram string operationXML = "<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>";
– Cédric Bignon
Feb 1 '13 at 12:16
1
@Pedram I have result.Message = "Job put in queue".
– Cédric Bignon
Feb 1 '13 at 12:43
|
show 8 more comments
1
"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.
– Christian.K
Feb 1 '13 at 12:10
1
If you set your xml example to operationXML. The deserialization works perfectly well.
– Cédric Bignon
Feb 1 '13 at 12:12
Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"
– disasterkid
Feb 1 '13 at 12:14
1
@Pedram string operationXML = "<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>";
– Cédric Bignon
Feb 1 '13 at 12:16
1
@Pedram I have result.Message = "Job put in queue".
– Cédric Bignon
Feb 1 '13 at 12:43
1
1
"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.
– Christian.K
Feb 1 '13 at 12:10
"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.
– Christian.K
Feb 1 '13 at 12:10
1
1
If you set your xml example to operationXML. The deserialization works perfectly well.
– Cédric Bignon
Feb 1 '13 at 12:12
If you set your xml example to operationXML. The deserialization works perfectly well.
– Cédric Bignon
Feb 1 '13 at 12:12
Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"
– disasterkid
Feb 1 '13 at 12:14
Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"
– disasterkid
Feb 1 '13 at 12:14
1
1
@Pedram string operationXML = "<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>";
– Cédric Bignon
Feb 1 '13 at 12:16
@Pedram string operationXML = "<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>";
– Cédric Bignon
Feb 1 '13 at 12:16
1
1
@Pedram I have result.Message = "Job put in queue".
– Cédric Bignon
Feb 1 '13 at 12:43
@Pedram I have result.Message = "Job put in queue".
– Cédric Bignon
Feb 1 '13 at 12:43
|
show 8 more comments
2 Answers
2
active
oldest
votes
Try this:
string xml = "<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>";
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(xml))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
Console.ReadKey();
Does it show "Job put in queue"?
add a comment |
AccountRequest xml = null;
var serializer = new XmlSerializer(typeof(AccountRequest));
using (TextReader reader = new StringReader(requestXml))
{
xml = (AccountRequest)serializer.Deserialize(reader);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14645964%2fdeserializing-xml-from-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
string xml = "<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>";
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(xml))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
Console.ReadKey();
Does it show "Job put in queue"?
add a comment |
Try this:
string xml = "<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>";
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(xml))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
Console.ReadKey();
Does it show "Job put in queue"?
add a comment |
Try this:
string xml = "<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>";
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(xml))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
Console.ReadKey();
Does it show "Job put in queue"?
Try this:
string xml = "<StatusDocumentItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>";
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(xml))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
Console.ReadKey();
Does it show "Job put in queue"?
answered Feb 1 '13 at 12:45
Cédric BignonCédric Bignon
10.5k13248
10.5k13248
add a comment |
add a comment |
AccountRequest xml = null;
var serializer = new XmlSerializer(typeof(AccountRequest));
using (TextReader reader = new StringReader(requestXml))
{
xml = (AccountRequest)serializer.Deserialize(reader);
}
add a comment |
AccountRequest xml = null;
var serializer = new XmlSerializer(typeof(AccountRequest));
using (TextReader reader = new StringReader(requestXml))
{
xml = (AccountRequest)serializer.Deserialize(reader);
}
add a comment |
AccountRequest xml = null;
var serializer = new XmlSerializer(typeof(AccountRequest));
using (TextReader reader = new StringReader(requestXml))
{
xml = (AccountRequest)serializer.Deserialize(reader);
}
AccountRequest xml = null;
var serializer = new XmlSerializer(typeof(AccountRequest));
using (TextReader reader = new StringReader(requestXml))
{
xml = (AccountRequest)serializer.Deserialize(reader);
}
answered Nov 23 '18 at 7:42
Varun Tej Reddy PutchakayalaVarun Tej Reddy Putchakayala
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14645964%2fdeserializing-xml-from-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"operationXML contains the string" - does it? Have you actually checked with, say, a debugger? "getJSON" to retrieve XML looks fishy.
– Christian.K
Feb 1 '13 at 12:10
1
If you set your xml example to operationXML. The deserialization works perfectly well.
– Cédric Bignon
Feb 1 '13 at 12:12
Yes it does contain the string, here's what I get from debugger: "<StatusDocumentItem xmlns:i="w3.org/2001/XMLSchema-instance"><DataUrl/><LastUpdated>2013-02-01T12:13:02.0997071Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>"
– disasterkid
Feb 1 '13 at 12:14
1
@Pedram string operationXML = "<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>";
– Cédric Bignon
Feb 1 '13 at 12:16
1
@Pedram I have result.Message = "Job put in queue".
– Cédric Bignon
Feb 1 '13 at 12:43