无法使用 XmlSerializer 类反序列化某一字段 xml
我有以下 xml:
<?xml version="1.0" encoding="UTF-8"?>
<connection_state>conn_state</connection_state>
按照 msdn,我必须将其描述为使用 XmlSerializer 正确反序列化的类型。所以类名指向第一个标签,其字段指向子标签。 例如:
public class connection_state
{
public string state;
}
会转变成下面的xml:
<?xml version="1.0" encoding="UTF-8"?>
<connection_state>
<state>conn_state</state>
</connection_state>
但是我收到的xml只有一个标签。我们不能用其类的名称创建一个字段,例如:
public class connection_state
{
public string connection_state;
}
或者可以吗? 这个问题有什么解决办法吗?
I have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<connection_state>conn_state</connection_state>
Following the msdn, I must describe it as a type for correct deserialization using XmlSerializer. So the class name points the first tag, and its fields subtags.
For example:
public class connection_state
{
public string state;
}
Will be transformed into the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<connection_state>
<state>conn_state</state>
</connection_state>
But the xml I receive has only one tag. And we cannot create a field with the name of its class like:
public class connection_state
{
public string connection_state;
}
Or can?
Is there any solution for this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的 Xml 有一个根元素,除了其他元素之外没有任何内容。如果您被那个微小的单标签伪 XML 所困扰,您是否有理由需要使用 XmlSerializer?为什么不直接创建一个带有接受文字“Xml”字符串的构造函数的类:
编辑:
响应OP的评论:您没有拥有给我们一个XmlSerializer;您可以直接读取 ResponseStream 并将其传递给您的
connection_state
构造函数:Proper Xml has a root element with no content except other elements. If you are stuck with that tiny one-tag psuedo-XML, is there a reason you need to use XmlSerializer? Why not just create a class with a constructor that takes the literal "Xml" string:
Edit:
In response to OP's comment: You don't have to us an XmlSerializer; you can just read the ResponseStream directly and pass that to your
connection_state
constructor:替换
为
Replace
to