【问题标题】:Take string received from Web Service and populate class获取从 Web 服务接收的字符串并填充类
【发布时间】:2011-07-14 19:06:12
【问题描述】:

我有一个使用 XSD 工具从 xsd 文件生成的类文件。我收到来自 Web 服务的 XML 字符串响应。如何使用收到的字符串 xml 填充生成的类?

【问题讨论】:

  • 您尝试使用哪种编程语言来填充类?
  • 如您所描述的填充类的过程通常称为解组。我对 C# 不熟悉,但 google 可能会帮助你输入这个术语。
  • 这是什么网络服务?您通常只需使用“添加服务引用”来指向服务的 WSDL,那么您根本不必玩 XML。

标签: xml web-services xsd


【解决方案1】:

您可以使用 System.Runtime.Serialization.DataContractSerializer 来生成类。

TheClass result = null;
DataContractSerializer dcs = new DataContractSerializer(typeof(TheClass));
using(StringReader reader = new StringReader(xml))
{
    using(XmlReader XmlReader = new XmlReader(reader))
    {
        result = dcs.ReadObject() as TheClass;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 2020-09-25
    • 2013-08-12
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多