C# .NET和SOAP标记问题(将对象序列化为XML)
快速提问:我正在创建一个XML souap信封,该信封应具有以下结构:C# .NET和SOAP标记问题(将对象序列化为XML),c#,asp.net,xml,api,soap,C#,Asp.net,Xml,Api,Soap,快速提问:我正在创建一个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/enve
<?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>
有人知道如何将这些soap:tag生成到我生成的XML中吗?我知道我可以将它们硬编码为字符串并添加参数(ID、令牌和日期),但我认为从类生成XML更干净。提前谢谢
有人知道如何将这些soap:tags生成到我生成的
XML
那些soap:
标记(如您所称)被称为名称空间
。您可以创建一个名称空间,然后在此名称空间下包含信封
和正文
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>
);
//声明Soap命名空间
XNS=@”http://schemas.xmlsoap.org/soap/envelope/";
//创建Soap信封
var doc=新XDocument(
新XDeclaration(“1.0”,“utf-8”,空),
新元素(ns+“信封”,
新的XAttribute(XNamespace.Xmlns+“xsi”,@”http://www.w3.org/2001/XMLSchema-instance"),
新的XAttribute(XNamespace.Xmlns+“xsd”,@”http://www.w3.org/2001/XMLSchema"),
新的XAttribute(XNamespace.Xmlns+“soap”,ns),
新元素(ns+“主体”,
新XElement(ns+“获取某物”,
新的XElement(“SecurityToken”,“{{SecurityToken}}”),
新的XElement(“ID”,“{{ID}”),
新的XElement(“StartDate”,“{{StartDate}”),
新的XElement(“EndDate”,“{{EndDate}}”)
)//
) //
)//
);
当您在XElement
中组合名称空间时,它将向XElement
名称(标记名)添加前缀。例如ns+“信封”
它将输出
,这意味着
元素位于soap
命名空间下
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; }
}
}
//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>
);