[英]How do I deserialize a soap response that is just one element?
So after I get my response I do (Where T
in this case is string
): 所以,在我得到响应后,我就开始了(在这种情况下, T
是string
):
var theResult = response.Content.ReadAsStringAsync().Result;
using (var stringreader = new StringReader(theResult))
{
var serializer = new XmlSerializer(typeof(T));
var responseObj = (T)serializer.Deserialize(stringreader);
}
theResult
is: theResult
是:
<string xmlns="http://tempuri.org/">Hello World</string>
When I do the Desrialize
I get an exception. 当我进行Desrialize
我得到一个例外。 <string xmlns='http://tempuri.org/'> was not expected.
as the inner exception and There is an error in XML document (2, 2).
作为内部异常,并且There is an error in XML document (2, 2).
as the message. 作为消息。
I am trying deserialize Hello World
to a string or deserialize to an object when I have multiple elements with a more than just a root element. 当我有多个元素而不仅仅是根元素时,我正在尝试将Hello World
反序列化为字符串或反序列化为对象。 When there is a root element with more members, I think I have that figured out. 当有一个具有更多成员的根元素时,我想我已经解决了。
In order to fix the issue, you need to create the serializer with the default namespace: 为了解决此问题,您需要使用默认名称空间创建序列化程序:
var serializer = new XmlSerializer(typeof(T), "http://tempuri.org/");
var responseObj = (T)serializer.Deserialize(stringreader);
You can read alittle more on this in the link below: 您可以在下面的链接中阅读更多有关此内容的内容:
https://msdn.microsoft.com/en-us/library/aa302290.aspx https://msdn.microsoft.com/zh-CN/library/aa302290.aspx
for example if your using a custome class you can define a specific classname for the XmlSerialier 例如,如果您使用定制类,则可以为XmlSerialier定义特定的类名
[XmlRoot( Namespace="urn:my-namespace" )]
public class MyClass
{
public string MyField;
}
You can read more on this in the link above. 您可以在上面的链接中阅读更多内容。
Your xml is not well framed since you have an array a root level. 您的xml框架格式不正确,因为您有一个数组的根级别。 You must allow you xml reader to allow for fragments. 您必须允许您的xml阅读器允许片段。
XmlSerializer xs = new XmlSerializer(typeof(AppConfig));
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(filepath);
AppConfig configData = (AppConfig)xs.Deserialize(reader);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.