[英].NET and SOAP tags question (serialization of object into XML)
快速問題:我正在創建一個應該具有以下結構的 XML souap 信封:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetSomething>
<SecurityToken>{{SecurityToken}}</SecurityToken>
<ID>{{ID}}</ID>
<StartDate>{{StartDate}}</StartDate>
<EndDate>{{EndDate}}</EndDate>
</GetSomething>
</soap:Body>
</soap:Envelope>
我已經能夠讓它看起來像這樣:
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<GetSomething>
<SecurityToken>{{SecurityToken}}</SecurityToken>
<ID>{{ID}}</ID>
<StartDate>{{StartDate}}</StartDate>
<EndDate>{{EndDate}}</EndDate>
</GetSomething>
</Body>
</Envelope>
...但由於某種原因,我無法讓它顯示肥皂:在正文和信封標簽之前,因此發送時請求將不起作用(帶有“肥皂:”標簽的請求將在我從郵遞員發送時,但如果我刪除它們則不會)。
下面是 Envelope 類的外觀:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Some.Namespace
{
[XmlRoot("Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class GetSomething
{
[XmlElement(ElementName = "SecurityToken", Namespace = "http://www.tempuri.com/")]
public string SecurityToken { get; set; }
[XmlElement(ElementName = "ID", Namespace = "http://www.tempuri.com/")]
public string ID { get; set; }
[XmlElement(ElementName = "StartDate", Namespace = "http://www.tempuri.com/")]
public string StartDate { get; set; }
[XmlElement(ElementName = "EndDate", Namespace = "http://www.tempuri.com/")]
public string EndDate { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
}
[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public class Body
{
public Body()
{
this.GetSomething = new GetSomething();
}
[XmlElement(ElementName = "GetSomething", Namespace = "http://www.tempuri.org/")]
public GetSomething GetSomething { get; set; }
}
[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public class Envelope
{
public Envelope()
{
this.Body = new Body();
}
[XmlElement(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Soap { get; set; }
}
}
有誰知道將這些soap: 標簽生成到我生成的XML 中嗎? 我知道我可以將它們硬編碼到一個字符串中並添加我的參數(ID、令牌和日期),但我認為從類生成 XML 更清晰。 提前致謝!
有誰知道如何將這些soap: 標簽生成到我生成的XML 中?
那些soap:
標簽(如您所稱)稱為Namespace
。 您可以創建一個命名空間,然后在該命名空間下包含Envelope
和Body
。
Soap 有一個默認命名空間http://schemas.xmlsoap.org/soap/envelope/
您可以在 .NET 中使用XmlDocument
或XDocument
。 在下面的示例中,我使用了XDocument
,這是我最喜歡的 XML 類。
//Declare Soap Namespace
XNamespace ns = @"http://schemas.xmlsoap.org/soap/envelope/";
//Create Soap Envelope
var doc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement(ns + "Envelope",
new XAttribute(XNamespace.Xmlns + "xsi", @"http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(XNamespace.Xmlns + "xsd", @"http://www.w3.org/2001/XMLSchema"),
new XAttribute(XNamespace.Xmlns + "soap", ns),
new XElement(ns + "Body",
new XElement(ns + "GetSomething",
new XElement("SecurityToken", "{{SecurityToken}}"),
new XElement("ID", "{{ID}}"),
new XElement("StartDate", "{{StartDate}}"),
new XElement("EndDate", "{{EndDate}}")
)//<GetSomething>
) //<soap:Body>
)//<soap:Envelope>
);
當您在XElement
組合命名空間時,它將為XElement
名稱(標記名稱)添加前綴。 例如ns + "Envelope"
它將輸出<soap:Envelope>
這意味着<Envelope>
元素在soap
命名空間下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.