反序列化字符串,不带任何xml声明和根元素 [英] deserialize string without any of xml declarations and root element

查看:88
本文介绍了反序列化字符串,不带任何xml声明和根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对以下字符串< q1> val1</q1>< q2> val2</q2>进行反序列化.....
我上课
< pre lang ="c#"> [可序列化]
公开课测试{

字符串q1 {get; set}
字符串q2 {get; set;}
.
.
.
}
//这是反序列化过程:
公开测试myTest(字符串XMLString){
var serializer = new XmlSerializer(typeof(test));
使用(var reader = new StringReader(XMLString)){
返回(测试)序列化器.Deserialize(读者);
}
}</pre>

我得到的错误是:
< code> XML文档(1、2)中有错误. ---& gt; System.InvalidOperationException:<状态xmlns ="> </status></code>

我知道问题在于字符串缺少xml标头和根元素,但我只是不知道如何反序列化.
另一个解决方案是在该字符串中添加另一个包含缺少部分的字符串.

How can I deserilize a following string <q1>val1</q1><q2>val2</q2>.....
I have class
<pre lang="c#">[Serializable]
public class test{

string q1{get;set}
string q2{get;set;}
.
.
.
}
//and here is deserialization process:
public test myTest( string XMLString) {
var serializer = new XmlSerializer( typeof( test) );
using ( var reader = new StringReader( XMLString) ) {
return (test) serializer.Deserialize( reader );
}
}</pre>

The error I''m getting is:
<code>There is an error in XML document (1, 2). ---&gt; System.InvalidOperationException: <status xmlns=""> was not expected.</status></code>

I know that the problem is that the string is missing the xml header and root element,but I just don''t know how to deserialize without.
The other solution will be to add to this string another string containing the missing part.
Thanks for the advice.

推荐答案

XML具有很多结构,因此您不能反序列化val1val2 ...等.

使用序列化器获取XML,您将看到缺少的内容以及为什么它不起作用.
XML has a lot of structure to it, so you cannot deserialize val1val2... etc.

Use the serializer to get the XML and you will see what is missing and why it will not work.


这篇关于反序列化字符串,不带任何xml声明和根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆