简体   繁体   English

.NET 和 SOAP 标签问题(将对象序列化为 XML)

[英].NET and SOAP tags question (serialization of object into XML)

quick question: I am working on creating a XML souap envelope that should have this structure:快速问题:我正在创建一个应该具有以下结构的 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>

I have been able to get it to look like this:我已经能够让它看起来像这样:

<?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>

...but for some reason I can't get it to show the soap: before the body and envelope tags, so the request will not work when sent (the one with the "soap:" tags will when I send it from Postman, but not if I remove them). ...但由于某种原因,我无法让它显示肥皂:在正文和信封标签之前,因此发送时请求将不起作用(带有“肥皂:”标签的请求将在我从邮递员发送时,但如果我删除它们则不会)。

Here's how the Envelope class looks:下面是 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; }
    }
}

Does anyone know hot to generate those soap: tags into my generated XML?有谁知道将这些soap: 标签生成到我生成的XML 中吗? I know I could hard code them into a string and add my parameters (ID, Token and dates) but I think it's cleaner to generate the XML from the class.我知道我可以将它们硬编码到一个字符串中并添加我的参数(ID、令牌和日期),但我认为从类生成 XML 更清晰。 Thanks in advance!提前致谢!

Does anyone know how to generate those soap: tags into my generated XML?有谁知道如何将这些soap: 标签生成到我生成的XML 中?

Those soap: tags (as you called them) is called Namespace .那些soap:标签(如您所称)称为Namespace you can create a namespace and then include the Envelope and Body under this namespace.您可以创建一个命名空间,然后在该命名空间下包含EnvelopeBody

Soap has a default namespace http://schemas.xmlsoap.org/soap/envelope/ You can work with XmlDocument or XDocument in .NET. Soap 有一个默认命名空间http://schemas.xmlsoap.org/soap/envelope/您可以在 .NET 中使用XmlDocumentXDocument in the following example I've used XDocument which is my favorite class for XML.在下面的示例中,我使用了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>
    );

when you combine the namespace in XElement , it'll add a prefix to the XElement name (tag name).当您在XElement组合命名空间时,它将为XElement名称(标记名称)添加前缀。 such as ns + "Envelope" it'll output <soap:Envelope> which means <Envelope> element is under soap namespace.例如ns + "Envelope"它将输出<soap:Envelope>这意味着<Envelope>元素在soap命名空间下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM