<soap:Envelope>
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"/span>
xmlns:bus="http://ws.praxedo.com/v6/businessEvent">
<soap:Header/>>
<soap:Body>/span>
<bus:listAttachments>/span>
<businessEventId>/span>00044</businessEventId>
</bus:listAttachments>/span>
</soap:Body>/span>
</soap:Envelope>/span>
顯然,要做到這一點的簡單方法只是創建一個字串,并將一些變數(例如businessEventId)插到它的中間,就像:
$"<soap: Envelope xmlns:soap="http:/span>/www. w3.org/2003/05/soap-envelope" "
" xmlns:bus="http:/span>/ws. praxedo.com/v6/businessEvent">/span>"
"<soap:Header/><soap:Body> "
"<bus: listAttachments><businessEventId>/span>{businessEventId}</businessEventId>"
"</bus:listAttachments></soap。 Body></soap:Envelope> "
但是我寧愿實體化一些POPO的集合,比如:
public class Envelope {
public Header Header {get; init; }
public Body 身體 {get; init; }
}
public class Header {
}
public class Body {
public ListAttachments Attachments {get; init; }
}
public class ListAttachments {
public string BusinessEventId {get; init; }
}
我需要宣告哪些屬性? 假設我已經有了一個填充的實體,那么我需要做什么來序列化它呢?
--
private string CreateBody(string businessEventId)
{
BusinessEventAttachmentListRequestEnvelope envelope = BusinessEventAttachmentListRequestEnvelope.From(businessEventId);
MemoryStream memorystream = new()。
DataContractSerializer serializer = new(typeof(BusinessEventAttachmentListRequestEnvelope))。
serializer.WriteObject(memorystream, envelope);
memorystream.Seek(0, SeekOrigin.Begin)。
using StreamReader streamReader = new(memorystream);
return streamReader.ReadToEnd()。
}
[DataContract(Name = "Envelope"/span>, Namespace = "http://www.w3.org/2003/05/soap-envelope"/span>)]
public class BusinessEventAttachmentListRequestEnvelope
{
[資料成員]
EmptySoapHeader Header = new();
[DataMember] 空肥皂頭 = new(;)
BusinessEventAttachmentListRequestBody Body { get; init; }
public static BusinessEventAttachmentListRequestEnvelope From(string businessEventId) =>
new()
{
Body = new()
{
Request = new()
{
BusinessEventId = businessEventId
}
}
};
}
[DataContract(Name = "Header"/span>, Namespace = "http://www.w3.org/2003/05/soap-envelope"/span>)]
public class EmptySoapHeader
{
}
[DataContract(Name = "Body"/span>, Namespace = "http://www.w3.org/2003/05/soap-envelope"/span>)]
public class BusinessEventAttachmentListRequestBody >。
{
[DataMember(Name = "listAttachments")]
public BusinessEventAttachmentListRequest Request { get; init; }
}
[DataContract(Name = "listAttachments"/span>, Namespace = "http://ws.praxedo.com/v6/businessEvent"/span>)]
public class BusinessEventAttachmentListRequest >。
{
[DataMember(Name = "businessEventId")]
public string BusinessEventId { get; init; }
產生的XML似乎有很大的不同:
<Envelope xmlns=http: //www. w3.org/2003/05/soap-envelope" xmlns:i="http:/span>/www. w3.org/2001/XMLSchema-instance" >
<Body>
<listAttachments xmlns:a="http:/span>/ws. praxedo.com/v6/businessEvent" >
<a:businessEventId>/span>00044</a:businessEventId>/span>
</listAttachments>/span>
</Body>/span>
<Header/>>
</Envelope>/span>
而遠程服務器回傳錯誤500。
uj5u.com熱心網友回復:
如果你使用Visual Studio,你可以使用一個非常簡單的快捷方式。將XML粘貼為類:
這就為你的樣本XML創建了:
// 注意:生成的代碼可能至少需要.NET Framework 4.5或.NET Core/Standard 2.0。
// < remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")/span>]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2003/05/soap-envelope"/span>)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2003/05/soap-envelope"/span>, IsNullable = false)]
public partial class Envelope
{
private object headerField;
private EnvelopeBody bodyField;
// < remarks/>
public object Header
{
get
{
return this.headerField。
}
set .headerField; }
{
this.headerField = value;
}
}
// < remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField。
}
set .bodyField; }
{
this.bodyField = value;
}
}
}
// < remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")/span>]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2003/05/soap-envelope"/span>)]
public partial class EnvelopeBody
{
private listAttachments listAttachmentsField;
// < remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://ws.praxedo.com/v6/businessEvent"/span>)]
public listAttachments listAttachments
{
get] get
{
return this.listAttachmentsField;
}
set .listAttachmentsField; }
{
this.listAttachmentsField = value;
}
}
}
// < remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")/span>]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://ws.praxedo.com/v6/businessEvent"/span>)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://ws.praxedo.com/v6/businessEvent"/span>, IsNullable = false)]
public partial class listAttachments
{
private byte businessEventIdField;
// < remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = ""/span>)]
public byte businessEventId
{
get businessEventId {
{
return this.businessEventIdField。
}
set .businessEventIdField; }
{
this.businessEventIdField = value;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332444.html
標籤:
上一篇:在bash中顯示睡眠時的進度

