为<govtalkmessage xmlns =“http://www.govtalk.gov.uk/CM/envelope”> </govtalkmessage>生成XML

时间:2014-12-01 06:07:03

标签: c#

请帮助我,我希望使用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>

事先谢谢你的回答

3 个答案:

答案 0 :(得分:0)

这是一个完整的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>

答案 1 :(得分:0)

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

拥有架构文件后,您可以使用Windows SDK中的XSD.exe工具生成服务类。如果您有多个模式文件(例如,一个用于GovTalkMessage包装器,一个用于特定域,如下面的示例中的Inland Revenue),您可以将它们组合在一个服务类文件中:

如果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;
            }

        }

希望这有助于您开始使用。

答案 2 :(得分:0)

  1. 复制整个XML(CRTL + A CTRL + C)
  2. 打开Visual Studio并创建新类
  3. 点击菜单 - 修改 - &gt;选择性粘贴 - &gt;将XML粘贴为类
  4. 之后,您可以使用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...