XML序列化问题使用C#将数据保存到Workday Web服务时

Posted

技术标签:

【中文标题】XML序列化问题使用C#将数据保存到Workday Web服务时【英文标题】:XML serialization issue While saving data to Workday Web-service with C# 【发布时间】:2020-03-31 22:30:08 【问题描述】:

我正在使用以下 WSDL 并尝试在 Workday 中保存

https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v18/Resource_Management.wsdl.

我已按照工作日 API 中提到的步骤操作(https://community.workday.com/articles/946)..that 是在生成的源代码中使用 [] 删除 [][] 之后。

我面临以下问题。System.InvalidOperationException:

'有一个错误反映'Submit_Supplier_Contract_Amendment_Response

InvalidOperationException:对于非数组类型,您可以使用以下属性:XmlAttribute、XmlText、XmlElement 或 XmlAnyElement。

这里是源代码-

    Resource_ManagementPortClient hr = new Resource_ManagementPortClient();

    hr.Endpoint.Address = new EndpointAddress("https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v18/Resource_Management.wsdl");

    //Specify the username and password for WS-Security UsernameToken Header
    hr.ClientCredentials.UserName.UserName = "xxx";
    hr.ClientCredentials.UserName.Password = "xxx";

    CompanyObjectIDType[] cid = new CompanyObjectIDType[1];
    cid[0] = new CompanyObjectIDType  type = "Company_Reference_ID", Value = "327" ;

    CurrencyObjectIDType[] currencyid = new CurrencyObjectIDType[1]  new CurrencyObjectIDType  type = "Currency_ID", Value = "USD"  ;
    Supplier_Invoice_RequestObjectIDType[] supplerrid = new Supplier_Invoice_RequestObjectIDType[1]  new Supplier_Invoice_RequestObjectIDType  type = "Supplier_ID", Value = "S-0000006695"  ;
    Payment_TermsObjectIDType[] paymentTerm = new Payment_TermsObjectIDType[1]  new Payment_TermsObjectIDType  type = "Payment_Terms_ID", Value = "NET0DAYS"  ;

    Spend_CategoryObjectType catobjtype = new Spend_CategoryObjectType()
    
        ID = new Spend_CategoryObjectIDType[] new Spend_CategoryObjectIDType 

                type="Spend_Category_ID",
                Value="SC0374"
             
    ;

    Accounting_WorktagObjectType[] worktagobj =
        new Accounting_WorktagObjectType[] new Accounting_WorktagObjectType  ID= new Accounting_WorktagObjectIDType[]  new Accounting_WorktagObjectIDType 
                type="Cost_Center_Reference_ID",
                Value="W5310"

               ;


    Supplier_Invoice_DataType sisd = new Supplier_Invoice_DataType();

    sisd.Submit = true;
    sisd.Company_Reference = new CompanyObjectType()  ID = cid ;
    sisd.Currency_Reference = new CurrencyObjectType()  ID = currencyid.ToArray() ;
    sisd.Supplier_Invoice_Request_Reference = new Supplier_Invoice_RequestObjectType()  ID = supplerrid.ToArray() ;
    sisd.Invoice_Date = DateTime.UtcNow;
    sisd.Suppliers_Invoice_Number = "SupplierInvoiceNumber000020";
    sisd.Payment_Terms_Reference = new Payment_TermsObjectType()  ID = paymentTerm.ToArray() ;
    sisd.Invoice_Line_Replacement_Data = new Supplier_Invoice_Line_Replacement_DataType[]  new Supplier_Invoice_Line_Replacement_DataType 
            Extended_Amount=1234.56M,
            Spend_Category_Reference= catobjtype,
            Worktags_Reference = worktagobj

     ;

    //Instantiate Header for the request
    Workday_Common_HeaderType header = new Workday_Common_HeaderType();
    header.Include_Reference_Descriptors_In_Response = false;
    header.Include_Reference_Descriptors_In_ResponseSpecified = false;

    Submit_Supplier_Invoice_RequestType SSIR = new Submit_Supplier_Invoice_RequestType();
    //  SSIR.Business_Process_Parameters = new Financials_Business_Process_ParametersType()  Auto_Complete = false ;
    SSIR.Supplier_Invoice_Data = sisd;
    SSIR.Business_Process_Parameters = new Financials_Business_Process_ParametersType()  Auto_Complete = false ;


    Exceptions_Response_Data.
    try
    
        var objres = hr.Submit_Supplier_Invoice(header, SSIR);
    
    catch (Exception ex)
    

        throw ex;
    


【问题讨论】:

【参考方案1】:

xsd 中有很多可选的定义。所以我做了以下

1) 在浏览器中访问以下网站:https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v18/Resource_Management.wsdl

2) 仅获取代码的模式部分并添加所需的命名空间所以我的 xsd 文件以:

<?xml version="1.0" encoding="UTF-8"?>
      <xsd:schema elementFormDefault="qualified" attributeFormDefault="qualified"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:wd="urn:com.workday/bsvc"
                  targetNamespace="urn:com.workday/bsvc">
         <xsd:element name="Validation_Fault" type="wd:Validation_FaultType"/>
         <xsd:element name="Processing_Fault" type="wd:Processing_FaultType"/>
         <xsd:element name="Submit_Supplier_Invoice_Request"
                      type="wd:Submit_Supplier_Invoice_RequestType"/>

3) 然后我在架构定义之后删除了所有东西,所以最后看起来像这样

               <xsd:element name="Calculated_Effort" minOccurs="0" maxOccurs="1">
                  <xsd:annotation>
                     <xsd:documentation>READ ONLY: Calculated effort for the assignment in the unit of time specified on the scenario.</xsd:documentation>
                  </xsd:annotation>
                  <xsd:simpleType>
                     <xsd:restriction base="xsd:decimal">
                       <xsd:totalDigits value="13"/>
                        <xsd:minInclusive value="0"/>
                        <xsd:fractionDigits value="3"/>
                     </xsd:restriction>
                  </xsd:simpleType>
               </xsd:element>
            </xsd:sequence>
         </xsd:complexType>
      </xsd:schema>

4) 将架构粘贴到文件 workday.xsd

5) 然后从 cmd.exe 使用 xsd.exe 实用程序,如下所示

.\xsd.exe -c -l:cs workday.xsd

6) 样品请求

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


namespace ConsoleApplication143

    class Program
    
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        
            //from sampleon following webpage
            //https://zappysys.com/blog/get-data-from-workday-in-ssis-using-soap-or-rest-api/
            //<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bsvc="urn:com.workday/bsvc">
            //   <soapenv:Header/>
            //   <soapenv:Body>
            //      <bsvc:Employee_Get>
            //         <bsvc:Employee_Reference>
            //            <bsvc:Integration_ID_Reference>
            //               <bsvc:ID>gero et</bsvc:ID>
            //            </bsvc:Integration_ID_Reference>
            //         </bsvc:Employee_Reference>
            //      </bsvc:Employee_Get>
            //   </soapenv:Body>
            //</soapenv:Envelope>
            Envelope envelope = new Envelope()
            
                header = new Header(),
                body = new Body()
                
                    employee_Get = new Employee_Get()
                    

                        employee_Reference = new Employee_Reference()
                        
                            integration_ID_Reference = new Integration_ID_Reference()
                            
                                ID = "gero et"
                            
                        
                    
                
            ;

            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("bsvc", "urn:com.workday/bsvc");
            namespaces.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);

            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            serializer.Serialize(writer, envelope, namespaces);

        
    

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Header header  get; set; 
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body body  get; set; 
    
    [XmlRoot(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Header
    
    
    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    
        [XmlElement(ElementName = "Employee_Get", Namespace = "urn:com.workday/bsvc")]
        public Employee_Get employee_Get  get; set; 
    
    [XmlRoot(ElementName = "Employee_Get", Namespace = "urn:com.workday/bsvc")]
    public class Employee_Get
    
        [XmlElement(ElementName = "Employee_Reference", Namespace = "urn:com.workday/bsvc")]
        public Employee_Reference employee_Reference  get; set; 
    
    [XmlRoot(ElementName = "Employee_Reference", Namespace = "urn:com.workday/bsvc")]
    public class Employee_Reference
    
        [XmlElement(ElementName = "Integration_ID_Reference", Namespace = "urn:com.workday/bsvc")]
        public Integration_ID_Reference integration_ID_Reference  get; set; 
    
    [XmlRoot(ElementName = "Integration_ID_Reference", Namespace = "urn:com.workday/bsvc")]
    public class Integration_ID_Reference
    
        [XmlElement(ElementName = "ID", Namespace = "urn:com.workday/bsvc")]
        public string ID  get; set; 
    

【讨论】:

@jdweng-感谢您的回答。您能否提供更多信息..我正在使用 WSDL ..我希望生成的代码有问题..我现在对 XSD 感到困惑。如果可能的话,如果您可以提供任何示例代码..非常感谢您的帮助...如果您可以参考,我已经上传了代码。 github.com/purusottamsaha/Submit_Supplier_invoice. 我更新了代码。见网页:zappysys.com/blog/…

以上是关于XML序列化问题使用C#将数据保存到Workday Web服务时的主要内容,如果未能解决你的问题,请参考以下文章

如何将 XML 映射到 C# 对象

从 C# .NET Core(特别是 Workday)调用 Java Web 服务。如何在soap请求中获取xml属性

C# 中那些常用的工具类(Utility Class)

可以将 XSLT 样式表添加到序列化的 XML 文档吗?

C# 保存对象

将 C# 模型序列化为 xml 数据