C# 发送xml报文到用友U8生成凭证系列一(配置信息)

C# 发送xml报文到用友U8生成凭证系列一(配置信息)
C# 发送xml报文到用友U8生成凭证系列二(基础代码)
C# 发送xml报文到用友U8生成凭证系列三(Modal代码)
C# 发送xml报文到用友U8生成凭证系列四(Biz代码)最终代码

在项目过程中,需要把平台上生成的凭证xml报文发送到用友U8中生成凭证,供财务好做账,因此就有此系列文章产生:本文将讲解前期准备工作。及要发送xml报文的格式。

项目主要包括如下:


本文将先讲解 u8voucher.xml、Web.config、U8EAI.asmx、EAIHandler.cs等代码

XML报文格式如下:

 <!--凭证 roottag:类型 billtype:系统用 docid:唯一编号 receiver:接收方 sender:发送方编码 proc:操作码 codeexcanged:导入时编码是否转换  exportneedexch导出时是否要根据对照表替换 renewproofno重新编号 -->
  <ufinterface account="002" billtype="gl" filename="" isexchange="Y" proc="add" receiver="8201" replace="Y" 
        roottag="" sender="glv" subbilltype="">
    <voucher id="6c26dc32-81d0-4b39-a355-5c15b16769b1">
      <voucher_head>
        <company>8201</company>
        <voucher_type>A</voucher_type>
        <fiscal_year>2018</fiscal_year>
        <accounting_period>01</accounting_period>
        <voucher_id />
        <attachment_number>1</attachment_number>
        <prepareddate>2018-01-25</prepareddate>
        <enter>5329068</enter>
        <cashier />
        <signature>N</signature>
        <checker />
        <posting_date />
        <posting_person />
        <operator />
        <revokeflag>N</revokeflag>
        <voucherkind>0</voucherkind>
        <voucher_making_system>结算平台</voucher_making_system>
        <memo1 />
        <memo2 />
        <reserve1 />
        <reserve2 />
      </voucher_head>
      <voucher_body>
        <entry>
          <entry_id>1</entry_id>
          <account_code>10020101</account_code>
          <abstract>收黑龙江省隆盛进出口有限公司ZP1801240157</abstract>
          <settlement>P01</settlement>
          <document_id />
          <document_date>2018-01-24</document_date>
          <currency>CNY</currency>
          <unit_price>1.00</unit_price>
          <exchange_rate1>0.0000</exchange_rate1>
          <exchange_rate2>1.0000</exchange_rate2>
          <debit_quantity>0.00</debit_quantity>
          <primary_debit_amount>922.00</primary_debit_amount>
          <secondary_debit_amount>0.00</secondary_debit_amount>
          <natural_debit_currency>922.00</natural_debit_currency>
          <credit_quantity>0.00</credit_quantity>
          <primary_credit_amount>0.00</primary_credit_amount>
          <secondary_credit_amount>0.00</secondary_credit_amount>
          <natural_credit_currency>0.00</natural_credit_currency>
          <auxiliary_accounting />
          <bill_type />
          <bill_id />
          <bill_date />
          <detail />
          <free1 />
          <freeitem1 />
          <freeitem2 />
          <freeitem3 />
          <freeitem4 />
          <freeitem5 />
          <freeitem6 />
          <freeitem7 />
          <freeitem8 />
          <freeitem9 />
          <freeitem10 />
          <freeitem11 />
          <freeitem12 />
          <freeitem13 />
          <freeitem14 />
          <freeitem15 />
          <freeitem16 />
          <freeitem17 />
          <freeitem18 />
          <freeitem19 />
          <freeitem20 />
          <freeitem21 />
          <freeitem22 />
          <freeitem23 />
          <freeitem24 />
          <freeitem25 />
          <freeitem26 />
          <freeitem27 />
          <freeitem28 />
          <freeitem29 />
          <freeitem30 />
        </entry>
        <entry>
          <entry_id>2</entry_id>
          <account_code>11220108</account_code>
          <abstract>收黑龙江省隆盛进出口有限公司报关费等</abstract>
          <settlement />
          <document_id>HX1801240002</document_id>
          <document_date>2018-01-24</document_date>
          <currency>CNY</currency>
          <unit_price>1.00</unit_price>
          <exchange_rate1>0.0000</exchange_rate1>
          <exchange_rate2>1.0000</exchange_rate2>
          <debit_quantity>0.00</debit_quantity>
          <primary_debit_amount>0.00</primary_debit_amount>
          <secondary_debit_amount>0.00</secondary_debit_amount>
          <natural_debit_currency>0.00</natural_debit_currency>
          <credit_quantity>0.00</credit_quantity>
          <primary_credit_amount>922.00</primary_credit_amount>
          <secondary_credit_amount>0.00</secondary_credit_amount>
          <natural_credit_currency>922.00</natural_credit_currency>
          <auxiliary_accounting>
            <item name="客商辅助核算">GT16110214</item>
          </auxiliary_accounting>
          <bill_type />
          <bill_id />
          <bill_date />
          <detail />
          <free1 />
          <freeitem1>HX1801240002</freeitem1>
          <freeitem2 />
          <freeitem3 />
          <freeitem4 />
          <freeitem5 />
          <freeitem6 />
          <freeitem7 />
          <freeitem8 />
          <freeitem9 />
          <freeitem10 />
          <freeitem11 />
          <freeitem12 />
          <freeitem13 />
          <freeitem14 />
          <freeitem15 />
          <freeitem16 />
          <freeitem17 />
          <freeitem18 />
          <freeitem19 />
          <freeitem20 />
          <freeitem21 />
          <freeitem22 />
          <freeitem23 />
          <freeitem24 />
          <freeitem25 />
          <freeitem26 />
          <freeitem27 />
          <freeitem28 />
          <freeitem29 />
          <freeitem30 />
        </entry>
      </voucher_body>
    </voucher>
  </ufinterface>
 <!-- 现将各数据项说明如下 


 voucher_head 凭证头:


       company                 公司的名称或编号,U8用户不用填写,给NC用户使用


    voucher_type         nvarchar(2)    凭证类别        *不能为空


    fiscal_year                  凭证所属的会计年度 *不能为空


    accounting_period     tinyint        所属的会计期间    *不能为空


    voucher_id         smallint    凭证号       *当节点ufinterface属性renewproofno="false"或renewproofno=""时,不能为空;renewproofno="true"时,可以为空


    attachment_number     smallint    所附单据的数量  


    date             datetime    制单日期      *不能为空


    enter             nvarchar(20)    制单人名称      


      cashier         nvarchar(20)    出纳名称       


      signature         bit        是否已签字      


      checker         nvarchar(20)    审核人名称       


    posting_date         datetime    记账日期       


      posting_person         nvarchar(20)    记账人名称       


    voucher_making_system     nvarchar(10)    制单系统标示,导入时必须填GL,即总帐系统单据      


       memo1             nvarchar (10)    备注1       


       memo2             nvarchar (10)    备注2       


       reserve1        nvarchar (20)    保留(外部凭证业务类型) 


       reserve2        nvarchar (50)    保留(外部凭证业务号)   


        revokeflag                作废凭证此字段填1,正常凭证不填,有错凭证填2


 voucher_body 凭证体:


     entry_id         smallint    分录号      


     account_code         nvarchar(15)    科目编码       


     abstract         nvarchar(60)    摘要           


      settlement         nvarchar (3)    结算方式,在科目是银行或往来时,可以填写此项    


     document_id         nvarchar (30)    票据号,在科目是银行或往来时,可以填写此项         


     document_date         datetime    票据日期,在科目是银行或往来时,可以填写此项      


     currency         nvarchar (8)    币种           


     unit_price                 单价,在科目有数量核算时,填写此项 


     exchange_rate1         float        汇率1 主辅币核算时使用 折辅汇率 原币*汇率1=辅币  NC用户使用


     exchange_rate2         float        汇率2 折主汇率 折本汇率 本币*汇率2=主币,U8用户使用


     debit_quantity         float        借方数量,在科目有数量核算时,填写此项     


     primary_debit_amount     money        原币借方发生额  


     secondary_debit_amount     money        辅币借方发生额     


     natural_debit_currency     money        本币借方发生额     *与本币贷方发生额不能同时为空


     credit_quantity     float        贷方数量,在科目有数量核算时,填写此项


     primary_credit_amount     money        原币贷方发生额     


     secondary_credit_amount money        辅币贷方发生额     


     natural_credit_currency money        本币贷方发生额   *与本币借方发生额不能同时为空    


     bill_type        nvarchar (20)    原始单据类型,导入凭证时填空     


      bill_id         nvarchar (30)    原始单据编号,导入凭证时填空      


      bill_date         datetime    原始单据日期,导入凭证时填空     


     auxiliary_accounting     辅助核算     


                dept_id     nvarchar (12)    部门        *科目有部门核算时不能为空


                cperson_id     nvarchar (8)     人员        *科目有个人核算时不能为空


                ccus_id     nvarchar (20)    客户        *科目有客户核算时不能为空      


                csup_id     nvarchar (12)    供应商        *科目有供应商核算时不能为空       


            item_class     nvarchar(2)     项目大类    *科目有项目核算时不能为空     


                citem_id     nvarchar (20)    项目档案    *科目有项目核算时不能为空                     


            operator      nvarchar (20)    业务员         


        define1     nvarchar 20    自定义字段1


        define2     nvarchar 20    自定义字段2


        define3     nvarchar 20    自定义字段3


        define4     datetime    自定义字段4


        define5     smallint    自定义字段5


        define6     datetime    自定义字段6


        define7     float        自定义字段7


        define8     nvarchar 4    自定义字段8


        define9     nvarchar 8    自定义字段9


        define10     nvarchar 60    自定义字段10


        define11     nvarchar 120    自定义字段11


        define12     nvarchar 120    自定义字段12


        define13     nvarchar 120    自定义字段13


        define14     nvarchar 120    自定义字段14


        define15     int        自定义字段15


        define16     float        自定义字段16    


        detail 明细


                cash_flow_statement         现金流量表


                cash_flow             现金流量   三个属性:


            cash_item          现金流量项目


            natural_debit_currency     借方金额


            natural_credit_currency 贷方金额


        code_remark_statement        科目备查簿


            code_remark i_id="1"    备查第一条记录


                item name=""     项目名称及其值,同一记录有多个item


-->

Config配置文件:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="NC2U8.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <appSettings file="Config/appSetting.config"/>
  <connectionStrings>
    <add name="UFDATA_023_2015ConnectionString" connectionString="Data Source=192.168.10.221;Initial Catalog=UFDATA_001;
Persist Security Info=True;User ID=sa;Password=sa123456" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <!--
      通过 <authentication> 节,可配置 
      ASP.NET 用于识别进入用户的 
      安全身份验证模式。
    -->
    <authentication mode="Windows"/>
    <!--
       通过 <customErrors> 节,可以配置
       在执行请求的过程中出现未处理的错误时要执行 
       的操作。具体而言,
       开发人员通过该节可配置要显示的 html 错误页,
       以代替错误堆栈跟踪。

       <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
         <error statusCode="403" redirect="NoAccess.htm" />
         <error statusCode="404" redirect="FileNotFound.htm" />
       </customErrors>
    -->
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
    </pages>
    <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
  </system.web>
  <applicationSettings>
    <NC2U8.Properties.Settings>
      <setting name="NC2U8_WebReference_XsnService" serializeAs="String">
        <value>http://192.163.122.244:8083/XersonService.asmx</value>
      </setting>
    </NC2U8.Properties.Settings>
  </applicationSettings>
  <system.codedom>
      <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
          <providerOption name="CompilerVersion" value="v3.5"/>
          <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>
  <system.webServer>
      <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <remove name="ScriptModule"/>
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated"/>
      <remove name="ScriptHandlerFactory"/>
      <remove name="ScriptHandlerFactoryAppServices"/>
      <remove name="ScriptResource"/>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

一般处理程序U8EAI.asmx代码:

 /// <summary>
    /// U8EAI 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://www.xxx.com.cn/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class U8EAI : System.Web.Services.WebService
    {        
        /// <summary>
        /// u8数据导入服务
        /// </summary>
        /// <param name="type">数据类型。凭证为voucher</param>
        /// <param name="data">传入的数据字符串</param>
        /// <returns></returns>
        [WebMethod]
        public string Action(string type,string data)
        {
            var handler = new EAIHandler();
            return System.Web.HttpUtility.HtmlDecode(handler.Send(type, data));
        }

    }

U8 EAI 处理类代码:

using NC2U8.Biz;
using NC2U8.Modal;
using System;
using System.Text;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
namespace NC2U8
{
    public class EAIHandler
    {
       /// <summary>
        /// 返回转换后的U8xml格式
       /// </summary>
       /// <param name="type">类型</param>
       /// <param name="vcNo">VC编号</param>
       /// <param name="data">返回的XML数据</param>
       /// <returns></returns>
        public string Send(string type,string data)
        {
            ResultInfo rInfo = new ResultInfo();
            string result;
            try
            { 
                var convertor = new Route.Router().GetConvertor(type);
                var accepter = new EaiAccepter(convertor);
                var splitXml = accepter.Send(data).Split('|');
                //返回凭证结果
                var ReturnU8xml = string.Empty;
                var successInfo = "false";
                XDocument doc = XDocument.Parse(splitXml[1]);
                var RssXml = from p in doc.Descendants("item") select p;
                if (RssXml.Count() > 0)
                {
                    //2016/11/10 修正
                    //判断一个节点是否存在,不存在则为保存成功或者其他凭证错误信息
                    if (RssXml.Attributes("key").Count() > 0)
                    {
                        foreach (var item in RssXml)
                        {//数据重复上传信息/其他错误
                            ReturnU8xml = splitXml[0] + "-" + item.Attribute("dsc").Value.ToString() + ",请检查结算凭证/U8(科目代码)及生成日期";
                        }
                        successInfo = "false";
                    }
                    else
                    {
                        foreach (var item in RssXml)
                        {
                            //凭证保存成功
                            if (item.Attribute("succeed").Value == "0")
                            {
                                ReturnU8xml = splitXml[0] + "-" + item.Attribute("voucher_type").Value.ToString() + "-" + item.Attribute("voucher_id").Value.ToString() + "," + item.Attribute("dsc").Value.ToString();
                                successInfo = "true";
                            }
                            //导入失败的
                            else
                            {
                                //<item key="" succeed="601" dsc="EAI数据源(default)@001连接失败,原因:口令不正确!" />
                                var entry_id = item.Attribute("entry_id").Value.ToString() == "" ? "0" : item.Attribute("entry_id").Value.ToString();
                                var xml = splitXml[0] + "-" + item.Attribute("voucher_type").Value.ToString() + "-" + item.Attribute("voucher_id").Value.ToString() + "|行号:"
                                    + entry_id + "~" + item.Attribute("dsc").Value.ToString() + ";\r\n"; //Environment.NewLine;
                                ReturnU8xml += xml;
                                successInfo = "false";
                            }
                        }
                    }
                }
                else
                {
                    ReturnU8xml = "未知错误,请检查结算凭证/U8(科目代码)及生成日期";
                    successInfo = "false";
                }
                rInfo.result = ReturnU8xml;
                rInfo.success = successInfo;//"true";
                rInfo.message = string.Empty;
            }
            catch(Exception ex)
            {
                rInfo.success = "false";
                rInfo.message = ex.Message;
                rInfo.result = string.Empty;
                //记录报错原因
                result = ex.Message;
            }
          
            using(var ms = new System.IO.MemoryStream())
            {
                XmlWriterSettings setting = new XmlWriterSettings();
                setting.Encoding = new UTF8Encoding(false);
                setting.Indent = true;
                using(var xw = XmlWriter.Create(ms,setting))
                {
                    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(ResultInfo));
                    //Create our own namespaces for the output
                    System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
                    //Add an empty namespace and empty value
                    ns.Add("", "");//去除节点中的xmlns:xsi 属性
                    serializer.Serialize(xw,rInfo,ns);
                    result = Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            return result;
        }

        public class ReslutU8Xml
        {

        }
    }
}

前期配置及基础代码大体如上,接下来将详细讲解 怎么转换xml节点

服务器用友U8 EAI配置:

IIS默认显示:


猜你喜欢

转载自blog.csdn.net/kongwei521/article/details/79702752
今日推荐