C# 反序列化SOAP响应
数据是从Apache web服务返回的,因此我无法使用C# 反序列化SOAP响应,c#,soap,C#,Soap,数据是从Apache web服务返回的,因此我无法使用添加服务引用,因为生成的类无法进行web服务调用或返回数据 我试图在C#NET中反序列化下面的SOAP响应。我可以验证XDocument语句是否正确地提取了我想要反序列化的实际响应(取自),但是当我检查响应对象时,Attribute1和MapItem列表都是null 任何帮助都将不胜感激 SOAP响应 <?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns
添加服务引用
,因为生成的类无法进行web服务调用或返回数据
我试图在C#NET中反序列化下面的SOAP响应。我可以验证XDocument
语句是否正确地提取了我想要反序列化的实际响应(取自),但是当我检查响应对象时,Attribute1
和MapItem
列表都是null
任何帮助都将不胜感激
SOAP响应
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:checkcompanyResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
<checkcompanyReturn xsi:type="ns2:Map" xmlns:ns2="http://xml.apache.org/xml-soap">
<item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<key xsi:type="soapenc:string">Entry1</key>
<value xsi:type="soapenc:string">Y</value>
</item>
<item>
<key xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Entry2</key>
<value xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">N</value>
</item>
</checkcompanyReturn>
</ns1:checkcompanyResponse>
</soapenv:Body>
</soapenv:Envelope>
请用这个型号
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace yournamespace
{
[XmlRoot(ElementName="key")]
public class Key {
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
[XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
}
[XmlRoot(ElementName="value")]
public class Value {
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
[XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
}
[XmlRoot(ElementName="item")]
public class Item {
[XmlElement(ElementName="key")]
public Key Key { get; set; }
[XmlElement(ElementName="value")]
public Value Value { get; set; }
[XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
}
[XmlRoot(ElementName="checkcompanyReturn")]
public class CheckcompanyReturn {
[XmlElement(ElementName="item")]
public List<Item> Item { get; set; }
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlAttribute(AttributeName="ns2", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns2 { get; set; }
}
[XmlRoot(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
public class CheckcompanyResponse {
[XmlElement(ElementName="checkcompanyReturn")]
public CheckcompanyReturn CheckcompanyReturn { get; set; }
[XmlAttribute(AttributeName="encodingStyle", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public string EncodingStyle { get; set; }
[XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns1 { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
public CheckcompanyResponse CheckcompanyResponse { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenv { get; set; }
[XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
}
}
使用系统;
使用System.Xml.Serialization;
使用System.Collections.Generic;
名称空间yournamespace
{
[XmlRoot(ElementName=“key”)]
公开类密钥{
[XmlAttribute(AttributeName=“type”,命名空间=”http://www.w3.org/2001/XMLSchema-instance")]
公共字符串类型{get;set;}
[XmlText]
公共字符串文本{get;set;}
[XmlAttribute(AttributeName=“soapenc”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Soapenc{get;set;}
}
[XmlRoot(ElementName=“value”)]
公共阶级价值{
[XmlAttribute(AttributeName=“type”,命名空间=”http://www.w3.org/2001/XMLSchema-instance")]
公共字符串类型{get;set;}
[XmlText]
公共字符串文本{get;set;}
[XmlAttribute(AttributeName=“soapenc”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Soapenc{get;set;}
}
[XmlRoot(ElementName=“item”)]
公共类项目{
[xmlement(ElementName=“key”)]
公钥{get;set;}
[xmlement(ElementName=“value”)]
公共值{get;set;}
[XmlAttribute(AttributeName=“soapenc”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Soapenc{get;set;}
}
[XmlRoot(ElementName=“checkcompanyReturn”)]
公共类CheckcompanyReturn{
[xmlement(ElementName=“item”)]
公共列表项{get;set;}
[XmlAttribute(AttributeName=“type”,命名空间=”http://www.w3.org/2001/XMLSchema-instance")]
公共字符串类型{get;set;}
[XmlAttribute(AttributeName=“ns2”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Ns2{get;set;}
}
[XmlRoot(ElementName=“checkcompanyResponse”,命名空间=”http://DefaultNamespace")]
公共类CheckcompanyResponse{
[XmlElement(ElementName=“checkcompanyReturn”)]
public CheckcompanyReturn CheckcompanyReturn{get;set;}
[XmlAttribute(AttributeName=“encodingStyle”,命名空间=”http://schemas.xmlsoap.org/soap/envelope/")]
公共字符串编码样式{get;set;}
[XmlAttribute(AttributeName=“ns1”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Ns1{get;set;}
}
[XmlRoot(ElementName=“Body”,命名空间=”http://schemas.xmlsoap.org/soap/envelope/")]
公共阶级团体{
[XmlElement(ElementName=“checkcompanyResponse”,命名空间=”http://DefaultNamespace")]
public-CheckcompanyResponse-CheckcompanyResponse{get;set;}
}
[XmlRoot(ElementName=“信封”,命名空间=”http://schemas.xmlsoap.org/soap/envelope/")]
公共类信封{
[XmlElement(ElementName=“Body”,命名空间=”http://schemas.xmlsoap.org/soap/envelope/")]
公共机构主体{get;set;}
[XmlAttribute(AttributeName=“soapenv”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Soapenv{get;set;}
[XmlAttribute(AttributeName=“xsd”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Xsd{get;set;}
[XmlAttribute(AttributeName=“xsi”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串Xsi{get;set;}
}
}
将myXmlSerializer
更新为信封响应=(信封)XmlSerializer.Deserialize(新文件流(fileName,FileMode.Open))
后,信封和正文被反序列化,但是response.Body.CheckcompanyResponse.CheckcompanyReturn
仍然是null
。我们可以做的另一件事是使用Xpath获取值这方面的最佳解决方案也是使用XSLT将Api响应Xml转换为自定义Xml格式,然后在尝试找出代码的错误后反序列化它,我使用了pass-XML作为类
函数,该函数生成了一个基本框架,现在填充了checkcompanyReturn
值。谢谢你给我指明了正确的方向。
string fileName = @"C:\Users\SLUKY\Desktop\test.xml";
XDocument document = XDocument.Load(fileName);
XName soapBody = XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(CheckCompanyResponse));
CheckCompanyResponse response = (CheckCompanyResponse)xmlSerializer.Deserialize(document.Descendants(soapBody)
.First()
.FirstNode
.CreateReader()
);
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace yournamespace
{
[XmlRoot(ElementName="key")]
public class Key {
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
[XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
}
[XmlRoot(ElementName="value")]
public class Value {
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
[XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
}
[XmlRoot(ElementName="item")]
public class Item {
[XmlElement(ElementName="key")]
public Key Key { get; set; }
[XmlElement(ElementName="value")]
public Value Value { get; set; }
[XmlAttribute(AttributeName="soapenc", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenc { get; set; }
}
[XmlRoot(ElementName="checkcompanyReturn")]
public class CheckcompanyReturn {
[XmlElement(ElementName="item")]
public List<Item> Item { get; set; }
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlAttribute(AttributeName="ns2", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns2 { get; set; }
}
[XmlRoot(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
public class CheckcompanyResponse {
[XmlElement(ElementName="checkcompanyReturn")]
public CheckcompanyReturn CheckcompanyReturn { get; set; }
[XmlAttribute(AttributeName="encodingStyle", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public string EncodingStyle { get; set; }
[XmlAttribute(AttributeName="ns1", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns1 { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="checkcompanyResponse", Namespace="http://DefaultNamespace")]
public CheckcompanyResponse CheckcompanyResponse { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenv { get; set; }
[XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
}
}