Generate XML for <GovTalkMessage xmlns="http://www.g

本文关键字:http www quot xmlns for XML lt GovTalkMessage Generate | 更新日期: 2023-09-27 18:32:47

请帮助我,我想使用 C# 生成根 XML 以发送到服务器。

对于类似的

<?xml version="1.0" encoding="utf-8"?>
  <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
    <EnvelopeVersion>2.0</EnvelopeVersion>
    <Header>
        <MessageDetails>
            <Class>SendDocument</Class>
            <Qualifier>request</Qualifier>
            <Function>submit</Function>
      <TransactionID>5</TransactionID>
            <CorrelationID></CorrelationID>
        </MessageDetails>
        <SenderDetails>
            <IDAuthentication>
                <SenderID>[USER]</SenderID>
                <Authentication>
                    <Method>clear</Method>
                    <Value>[PASS]</Value>
                </Authentication>
            </IDAuthentication>
        </SenderDetails>
    </Header>
    <Body>
        <Message xmlns="urn:g3.ge:dea:call:SendDocument:v1">
           ................
        </Message>
    </Body>
  </GovTalkMessage>

事先感谢您的回答

Generate XML for <GovTalkMessage xmlns="http://www.g

首先,您需要为 XML 生成 XSD 架构。政府网关应为您的特定提交域提供 XSD。如果没有,则可以使用 XML 菜单中"创建架构"下的 Visual Studio XML 工具从 XML 文件生成一个相当好的架构。

获得架构文件后,可以使用 Windows SDK 中的 XSD.exe 工具来生成服务类。如果您有多个架构文件(例如,一个用于 GovTalkMessage 包装器,一个用于特定域,如下面示例中的税务局(,您可以将它们组合到一个服务类文件中:

如果 XSD 工具安装在其典型位置,则可以运行以下命令:

 C:'Program Files (x86)'Microsoft SDKs'Windows'v7.0A'Bin'x64>xsd.exe /c c:'Schemas'GovTalkMessage.xsd C:'ServiceClasses'IREnvelope.xsd /o:C:'GovTalkApplication

一旦有了 XML 的服务类,您就可以从数据创建新对象并相当容易地将它们序列化为 XML。在此代码示例中,我使用配置文件中的 AppSettings 来存储各种常量值,并使用名为 _localData 的私有 DataTable 来保存我放入 IR R68 有效负载(在元素中(的身体数据。我也在这里使用log4net记录器,这是偶然的。CreateIRBody 方法是您必须为需要包含的任何有效负载编写的东西:

   public GovTalkMessage CreateRequestMessage(string env)
        {
            try
            {
                log.Debug("CreateRequestMessage called");
                GovTalkMessageBody Body = new GovTalkMessageBody();
                IR68 ir68 = new IR68(_localData);
                log.Debug("Creating IR68 body ...");
                IRenvelope ire = new IRenvelope();
                ire = ir68.CreateIRBody();
                //serialize ire into XmlElement and then set Body.Any = 
                XmlElement xe = GiftAidSubmissionProcessController.SerializeIREnvelope(ire);
                XmlElement[] XmlElementIRenvelope = new XmlElement[1];
                XmlElementIRenvelope[0] = xe;
                Body.Any = XmlElementIRenvelope;
                //GovTalkDetails
                GovTalkMessageGovTalkDetailsChannelRoutingChannel Channel = new GovTalkMessageGovTalkDetailsChannelRoutingChannel();
                Channel.Version = ConfigurationManager.AppSettings["ChannelVersion"];
                Channel.Product = ConfigurationManager.AppSettings["ChannelProduct"];
                Channel.ItemElementName = ItemChoiceType.URI;
                Channel.Item = ConfigurationManager.AppSettings["ChannelURI"];
                GovTalkMessageGovTalkDetailsChannelRouting ChannelRouting = new GovTalkMessageGovTalkDetailsChannelRouting();
                ChannelRouting.Channel = Channel;
                GovTalkMessageGovTalkDetailsKey Key = new GovTalkMessageGovTalkDetailsKey();
                Key.Type = ConfigurationManager.AppSettings["GovTalkDetailsKeyType"];
                Key.Value = ConfigurationManager.AppSettings["GovTalkDetailsKey"];
                GovTalkMessageGovTalkDetails Details = new GovTalkMessageGovTalkDetails();
                GovTalkMessageGovTalkDetailsChannelRouting[] ChannelRoutings = 
                    new GovTalkMessageGovTalkDetailsChannelRouting[1];
                ChannelRoutings[0] = ChannelRouting;
                Details.ChannelRouting = ChannelRoutings;
                GovTalkMessageGovTalkDetailsKey[] Keys = new GovTalkMessageGovTalkDetailsKey[1];
                Keys[0] = Key;
                Details.Keys = Keys;
                string[] TargetDetails = new string[1];
                TargetDetails[0] = ConfigurationManager.AppSettings["GovTalkDetailsTargetOrganistion"];
                Details.TargetDetails = TargetDetails;
                //GovTalk Header
                GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication Authentication = new GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication();
                switch (ConfigurationManager.AppSettings["SenderAuthenticationMethod"])
                {
                    case "MD5":
                        Authentication.Method = GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthenticationMethod.MD5;
                        break;
                    case "clear":
                        Authentication.Method = GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthenticationMethod.clear;
                        break;
                    case "W3Csigned":
                        Authentication.Method = GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthenticationMethod.W3Csigned;
                        break;
                }
                Authentication.Role = ConfigurationManager.AppSettings["SenderAuthenticationRole"];
                Authentication.Item = ConfigurationManager.AppSettings["SenderAuthenticationValue"];
                GovTalkMessageHeaderSenderDetailsIDAuthentication IDAuthentication = new GovTalkMessageHeaderSenderDetailsIDAuthentication();
                IDAuthentication.SenderID = ConfigurationManager.AppSettings["SenderID"];
                GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication[] Authentications = new GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication[1];
                Authentications[0] = Authentication;
                IDAuthentication.Authentication = Authentications;
                GovTalkMessageHeaderSenderDetails SenderDetails = new GovTalkMessageHeaderSenderDetails();
                SenderDetails.IDAuthentication = IDAuthentication;
                GovTalkMessageHeaderMessageDetails MessageDetails = new GovTalkMessageHeaderMessageDetails();
                MessageDetails.Class = ConfigurationManager.AppSettings["MessageDetailsClass"];
                MessageDetails.Qualifier = GovTalkMessageHeaderMessageDetailsQualifier.request;
                MessageDetails.FunctionSpecified = true;
                MessageDetails.Function = GovTalkMessageHeaderMessageDetailsFunction.submit;
                MessageDetails.TransformationSpecified = true;
                MessageDetails.Transformation = GovTalkMessageHeaderMessageDetailsTransformation.XML;
                MessageDetails.GatewayTest = ConfigurationManager.AppSettings["MessageDetailsGatewayTest"];
                if (env == "local")
                {
                    MessageDetails.GatewayTimestampSpecified = true;
                    MessageDetails.GatewayTimestamp = DateTime.Now;
                }
                else
                {
                    MessageDetails.GatewayTimestampSpecified = false;
                    MessageDetails.GatewayTimestamp = DateTime.MinValue; }
                GovTalkMessageHeader Header = new GovTalkMessageHeader();
                Header.MessageDetails = MessageDetails;
                Header.SenderDetails = SenderDetails;
                GovTalkMessage GovTalkMessage = new hmrcclasses.GovTalkMessage();
                GovTalkMessage.EnvelopeVersion = ConfigurationManager.AppSettings["GovTalkMessageEnvelopeVersion"];
                GovTalkMessage.Header = Header;
                GovTalkMessage.GovTalkDetails = Details;
                GovTalkMessage.Body = Body;
                log.Info("GovTalkMessage created successfully");
                return GovTalkMessage;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
        }

希望这可以帮助您入门。

  1. 复制整个 XML (CRTL+A CTRL+C(
  2. 打开 Visual Studio 并创建新类
  3. 单击菜单 - 编辑 -> 选择性粘贴 -> 将 XML 粘贴为类

之后,您可以使用 XmlSerializer 生成该 XML

var entity = new GovTalkMessage();
//fill entity properties with data
var writer = new StringWriter();
var serializer = new XmlSerializer(typeof(GovTalkMessage));
serializer.Serialize(writer, entity);
var xmlText = writer.ToString();
//and so on...

这是一个完整的XML文件。

 <?xml version="1.0" encoding="utf-8"?>
    <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
        <EnvelopeVersion>2.0</EnvelopeVersion>
        <Header>
            <MessageDetails>
                <Class>SendDocument</Class>
                <Qualifier>request</Qualifier>
                <Function>submit</Function>
                <CorrelationID></CorrelationID>
            </MessageDetails>
            <SenderDetails>
                <IDAuthentication>
                    <SenderID>[USER]</SenderID>
                    <Authentication>
                        <Method>clear</Method>
                        <Value>[PASS]</Value>
                    </Authentication>
                </IDAuthentication>
            </SenderDetails>
        </Header>
        <Body>
            <Message xmlns="urn:g3.ge:dea:call:SendDocument:v1">
                <Container xmlns="http://www.dea.gov.ge/schemas/2011/dx">
                    <Reference>GUID</Reference>
                    <DocDate>yyyy-MM-dd HH:mm:ss</DocDate>
                    <DocNumber>string</DocNumber>
                    <Persons>
                        <Person>
                            <OriginID>1</OriginID>
                            <FirstName>string</FirstName>
                            <LastName>string</LastName>
                            <Position>string</Position>
                            <PersonType>1</PersonType>
                            <ResolutionText>string (max length: 1000 chars)</ResolutionText>
                            <OrgUnit>string (max length: 4000 chars)</OrgUnit>
                        </Person>
                        <Person>
                            <OriginID>447</OriginID>
                            <FirstName>string</FirstName>
                            <LastName>string</LastName>
                            <Position>string</Position>
                            <PersonType>2</PersonType>
                            <ResolutionText>string (max length: 1000 chars)</ResolutionText>
                            <OrgUnit>string (max length: 4000 chars)</OrgUnit>
                        </Person>
                    </Persons>
                    <Subject>string</Subject>
                    <MainDoc>
                        <Doc>
                            <FileName></FileName>
                            <ContentType></ContentType>
                            <Signature>
                                <Type></Type>
                                <Data>BASE64BINARY</Data>
                            </Signature>
                            <Data>BASE64BINARY</Data>
                            <DocType></DocType>
                        </Doc>
                    </MainDoc>
                    <RespondTo>
                        <OriginalDoc>
                            <Reference>GUID</Reference>
                            <Type/>
                        </OriginalDoc>
                    </RespondTo>
                    <OriginalReference>GUID</OriginalReference>
                    <AttachedDocs>
                        <Doc>
                            <FileName></FileName>
                            <ContentType></ContentType>
                            <Signature>
                                <Type></Type>
                                <Data>BASE64BINARY</Data>
                            </Signature>
                            <Data>BASE64BINARY</Data>
                            <DocType></DocType>
                        </Doc>
                        <Doc>
                            <FileName></FileName>
                            <ContentType></ContentType>
                            <Signature>
                                <Type></Type>
                                <Data>BASE64BINARY</Data>
                            </Signature>
                            <Data>BASE64BINARY</Data>
                            <DocType></DocType>
                        </Doc>
                        <Doc>
                            <FileName></FileName>
                            <ContentType></ContentType>
                            <Signature>
                                <Type></Type>
                                <Data>BASE64BINARY</Data>
                            </Signature>
                            <Data>BASE64BINARY</Data>
                            <DocType></DocType>
                        </Doc>
                    </AttachedDocs>
                    <Signature>
                        <Type></Type>
                        <Data>BASE64BINARY</Data>
                    </Signature>
                </Container>
            </Message>
        </Body>
    </GovTalkMessage>