Как создать c# класс модели для генерации конкретного XML - PullRequest
0 голосов
/ 19 марта 2020

Я создаю сервис WCF. Я хочу создать класс модели, который принимает запрос XML следующим образом.

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1r3="http://testsite.net/v1r3">
   <soapenv:Header/>
   <soapenv:Body>
      <v1r3:registerApp>
         <request>
            <header channel="?" componentId="?" lang="?" operatorId="?" timestamp="?" timeout="?" subver="?">
               <msgId>?</msgId>
            </header>
            <issuer issId="?" appId="?"/>
            <!--You have a CHOICE of the next 2 items at this level-->
            <!--Optional:-->
            <msisdnDigest>?</msisdnDigest>
            <app>
               <!--Optional:-->
               <ibanEncr serialNumer="?">cid:1412419884089</ibanEncr>
               <!--Optional:-->
               <appEncrData serialNumer="?">cid:333152353834</appEncrData>
               <!--Optional:-->
               <appLabel>?</appLabel>
               <!--Optional:-->
               <appType>?</appType>
            </app>
            <autoActivateFlg>true</autoActivateFlg>
         </request>
      </v1r3:registerApp>
   </soapenv:Body>
</soapenv:Envelope>

То, что я пробовал.

  1. XML2Csharp

Генерирует класс, как показано ниже, который не генерирует тот же XML.

namespace Xml2CSharp
{
    [XmlRoot(ElementName="header")]
    public class Header {
        [XmlElement(ElementName="msgId")]
        public string MsgId { get; set; }
        [XmlAttribute(AttributeName="channel")]
        public string Channel { get; set; }
        [XmlAttribute(AttributeName="componentId")]
        public string ComponentId { get; set; }
        [XmlAttribute(AttributeName="lang")]
        public string Lang { get; set; }
        [XmlAttribute(AttributeName="operatorId")]
        public string OperatorId { get; set; }
        [XmlAttribute(AttributeName="timestamp")]
        public string Timestamp { get; set; }
        [XmlAttribute(AttributeName="timeout")]
        public string Timeout { get; set; }
        [XmlAttribute(AttributeName="subver")]
        public string Subver { get; set; }
    }

    [XmlRoot(ElementName="issuer")]
    public class Issuer {
        [XmlAttribute(AttributeName="issId")]
        public string IssId { get; set; }
        [XmlAttribute(AttributeName="appId")]
        public string AppId { get; set; }
    }

    [XmlRoot(ElementName="ibanEncr")]
    public class IbanEncr {
        [XmlAttribute(AttributeName="serialNumer")]
        public string SerialNumer { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="appEncrData")]
    public class AppEncrData {
        [XmlAttribute(AttributeName="serialNumer")]
        public string SerialNumer { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="app")]
    public class App {
        [XmlElement(ElementName="ibanEncr")]
        public IbanEncr IbanEncr { get; set; }
        [XmlElement(ElementName="appEncrData")]
        public AppEncrData AppEncrData { get; set; }
        [XmlElement(ElementName="appLabel")]
        public string AppLabel { get; set; }
        [XmlElement(ElementName="appType")]
        public string AppType { get; set; }
    }

    [XmlRoot(ElementName="request")]
    public class Request {
        [XmlElement(ElementName="header")]
        public Header Header { get; set; }
        [XmlElement(ElementName="issuer")]
        public Issuer Issuer { get; set; }
        [XmlElement(ElementName="msisdnDigest")]
        public string MsisdnDigest { get; set; }
        [XmlElement(ElementName="app")]
        public App App { get; set; }
        [XmlElement(ElementName="autoActivateFlg")]
        public string AutoActivateFlg { get; set; }
    }

    [XmlRoot(ElementName="registerApp", Namespace="http://testsite.net/v1r3")]
    public class RegisterApp {
        [XmlElement(ElementName="request")]
        public Request Request { get; set; }
    }

    [XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body {
        [XmlElement(ElementName="registerApp", Namespace="http://testsite.net/v1r3")]
        public RegisterApp RegisterApp { get; set; }
    }

    [XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope {
        [XmlElement(ElementName="Header", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [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="v1r3", Namespace="http://www.w3.org/2000/xmlns/")]
        public string V1r3 { get; set; }
    }
XSD.exe SvcUtil.exe Специальная вставка Visual Studio (Вставить XML как классы)

Сгенерированный класс модели создает другой XML, как показано ниже.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IRegister/RegisterApp</Action>
  </s:Header>
  <s:Body>
    <RegisterApp xmlns="http://tempuri.org/">
      <model xmlns:d4p1="http://schemas.datacontract.org/2004/07/Test.test" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <d4p1:Body>
          <d4p1:RegisterApp>
            <d4p1:Request>
              <d4p1:App>
                <d4p1:AppEncrData i:nil="true" />
                <d4p1:AppLabel i:nil="true" />
                <d4p1:AppType i:nil="true" />
                <d4p1:IbanEncr i:nil="true" />
              </d4p1:App>
              <d4p1:AutoActivateFlg i:nil="true" />
              <d4p1:Header>
                <d4p1:Channel i:nil="true" />
                <d4p1:ComponentId i:nil="true" />
                <d4p1:Lang i:nil="true" />
                <d4p1:MsgId i:nil="true" />
                <d4p1:OperatorId i:nil="true" />
                <d4p1:Subver i:nil="true" />
                <d4p1:Timeout i:nil="true" />
                <d4p1:Timestamp i:nil="true" />
              </d4p1:Header>
              <d4p1:Issuer>
                <d4p1:AppId i:nil="true" />
                <d4p1:IssId i:nil="true" />
              </d4p1:Issuer>
              <d4p1:MsisdnDigest i:nil="true" />
            </d4p1:Request>
          </d4p1:RegisterApp>
        </d4p1:Body>
        <d4p1:Header i:nil="true" />
        <d4p1:Soapenv i:nil="true" />
        <d4p1:V1r3 i:nil="true" />
      </model>
    </RegisterApp>
  </s:Body>
</s:Envelope>

, что отличается от первого.

Я создал простой сервис WCF, как показано ниже, где я хочу принять первый XML привязать к этой модели.

[ServiceContract]
    public interface IRegister
    {
        [OperationContract]
        RegisterAppResponse RegisterApp(Xml2CSharp.Envelope model);

    }

Любой способ создать класс модели, который будет принимать xml запросов, как указано выше?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...