C# 从字符串反序列化 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14645964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:34:41  来源:igfitidea点击:

Deserializing XML from String

c#.netxmlxml-deserialization

提问by disasterkid

I'm trying to convert the result I get from my web service as a string and convert it to an object.

我正在尝试将从我的 Web 服务获得的结果转换为字符串并将其转换为对象。

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):

这就是我如何尝试使用 XMLDeserializer 将该字符串作为 StatusDocumentItem 类型的对象获取(注意,operationXML 包含字符串):

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:

更新。我从 operationXML 获得的值是这样的,并且有一个不必要的 xmlns 属性阻止了我的反序列化。没有该属性,一切正常。这是它的样子:

"<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>"

采纳答案by Cédric Bignon

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"?

它是否显示“作业放入队列”?