无法使用 XmlSerializer 类反序列化某一字段 xml

发布于 2024-12-23 09:24:35 字数 662 浏览 2 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇年纸鸢 2024-12-30 09:24:35

正确的 Xml 有一个根元素,除了其他元素之外没有任何内容。如果您被那个微小的单标签伪 XML 所困扰,您是否有理由需要使用 XmlSerializer?为什么不直接创建一个带有接受文字“Xml”字符串的构造函数的类:

using System.Xml.Linq;

public class connection_state {
    public string state { get; set; }
    public connection_state(string xml) {
        this.state = XDocument.Parse(xml).Element("connection_state").Value;
    }
}

编辑:

响应OP的评论:您没有拥有给我们一个XmlSerializer;您可以直接读取 ResponseStream 并将其传递给您的 connection_state 构造函数:

String xmlString = (new StreamReader(webResponse.GetResponseStream())).ReadToEnd();
connection_state c= new connection_state(xmlString);

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:

using System.Xml.Linq;

public class connection_state {
    public string state { get; set; }
    public connection_state(string xml) {
        this.state = XDocument.Parse(xml).Element("connection_state").Value;
    }
}

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:

String xmlString = (new StreamReader(webResponse.GetResponseStream())).ReadToEnd();
connection_state c= new connection_state(xmlString);
梅倚清风 2024-12-30 09:24:35

替换

public class connection_state
{
   public string state;
}

public class connection_state
{
   public string state {set; get;}
}

Replace

public class connection_state
{
   public string state;
}

to

public class connection_state
{
   public string state {set; get;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文