如何淡化xml并通过c#中的wcf服务传递它

Posted

技术标签:

【中文标题】如何淡化xml并通过c#中的wcf服务传递它【英文标题】:How to desalinize an xml and pass it through wcf service in c# 【发布时间】:2020-07-02 14:44:18 【问题描述】:

我有一个xml,如下所示,并希望将其作为来自wcf 服务的响应传递。我所做的是,我使用Visual Studio 从xml 生成c# 类,然后使用从 VS.Data 生成的类来自 WCF 服务的 Soap UI,但是当我在soap ui 中单击验证时,它会抛出错误

在 250320 上编辑

最后我遇到了一个错误,比如 第 17 行:元素 SITE_NAME@http://schemas.datacontract.org/2004/07/ITSM_GIS_NRMIntegration.BusinessObjects 中的无效 xsi:type qname:'c:string' c#代码

namespace ITSM_GIS_NRMIntegration

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class GisItsmService : IGisItsmService
    
      Datatable   dtcustomerSiteDtls = dtcustomerSiteDtls.AsEnumerable()
            .OrderBy(x => x.Field<string>("CBCM_PARTY_ID"))
            .ThenBy(x => x.Field<string>("CBCM_PARTY_NAME"))
            .ThenBy(x => x.Field<string>("SERVICE"))
            .ThenBy(x => x.Field<string>("SITENAME"))
            .ThenBy(x => x.Field<string>("NODENAME"))
            .CopyToDataTable();
                XElement allSites = doc.Root;
    
                foreach (var idGroup in dtcustomerSiteDtls.AsEnumerable().GroupBy(x => x.Field<string>("CBCM_PARTY_ID")))
                
                    XElement siteNode = new XElement("PARTY_SITE_NODES");
                    allSites.Add(siteNode);
    
    
                    siteNode.Add(new XElement("CBCM_PARTY_ID", idGroup.Key));
                    siteNode.Add(new XElement("CBCM_PARTY_NAME", idGroup.First().Field<string>("CBCM_PARTY_NAME")));
    
                    DataTable dtfilter = dtcustomerSiteDtls.Select("CBCM_PARTY_ID = '" + idGroup.Key.ToString() + "'").CopyToDataTable();
    
                    foreach (var service in dtfilter.AsEnumerable().GroupBy(x => x.Field<string>("SERVICE")))
                    
                        XElement partyServices = new XElement("PARTY_SERVICES");
                        siteNode.Add(partyServices);
                        partyServices.Add(new XElement("SERVICE_NAME", service.Key));
    
                        XElement serviceSites = new XElement("SERVICE_SITES");
                        partyServices.Add(serviceSites);
                        foreach (var serviceSite in service.GroupBy(x => x.Field<string>("SITENAME")))
                        
                            serviceSites.Add(new XElement("SITE_NAME", serviceSite.Key));
    
                            XElement siteNodes = new XElement("SITE_NODES");
                            serviceSites.Add(siteNodes);
    
                            string[] nodeNames = serviceSite.Select(x => x.Field<string>("NODENAME")).Distinct().ToArray();
                            foreach (string nodeName in nodeNames)
                            
                                siteNodes.Add(new XElement("NODE_NAME", nodeName));
                            
                        
                    
    
    
    
    
                
            
        
        catch (Exception ex)
        
            log.Error(ex.Message);
        
        XmlSerializer myItemSerializer = new XmlSerializer(typeof(getCustomerSites));
        using (var reader = doc.CreateReader())
        
            sitedetailsResObj = (getCustomerSites)myItemSerializer.Deserialize(reader);
        

用于序列化的 C# 类

namespace ITSM_GIS_NRMIntegration.BusinessObjects

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class getCustomerSites:ResponseBase
    

        private getCustomerSitesPARTY_SITE_NODES[] pARTY_SITE_NODESField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("PARTY_SITE_NODES")]
        public getCustomerSitesPARTY_SITE_NODES[] PARTY_SITE_NODES
        
            get
            
                return this.pARTY_SITE_NODESField;
            
            set
            
                this.pARTY_SITE_NODESField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class getCustomerSitesPARTY_SITE_NODES
    

        private uint cBCM_PARTY_IDField;

        private string cBCM_PARTY_NAMEField;

        private getCustomerSitesPARTY_SITE_NODESPARTY_SERVICES[] pARTY_SERVICESField;

        /// <remarks/>
        public uint CBCM_PARTY_ID
        
            get
            
                return this.cBCM_PARTY_IDField;
            
            set
            
                this.cBCM_PARTY_IDField = value;
            
        

        /// <remarks/>
        public string CBCM_PARTY_NAME
        
            get
            
                return this.cBCM_PARTY_NAMEField;
            
            set
            
                this.cBCM_PARTY_NAMEField = value;
            
        

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("PARTY_SERVICES")]
        public getCustomerSitesPARTY_SITE_NODESPARTY_SERVICES[] PARTY_SERVICES
        
            get
            
                return this.pARTY_SERVICESField;
            
            set
            
                this.pARTY_SERVICESField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class getCustomerSitesPARTY_SITE_NODESPARTY_SERVICES
    

        private string sERVICE_NAMEField;

        private getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITES sERVICE_SITESField;

        /// <remarks/>
        public string SERVICE_NAME
        
            get
            
                return this.sERVICE_NAMEField;
            
            set
            
                this.sERVICE_NAMEField = value;
            
        

        /// <remarks/>
        public getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITES SERVICE_SITES
        
            get
            
                return this.sERVICE_SITESField;
            
            set
            
                this.sERVICE_SITESField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [KnownType(typeof(getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITESSITE_NODES))]
    public partial class getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITES
    

        private object[] sITE_NAMEField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("SITE_NAME", typeof(string))]
        [System.Xml.Serialization.XmlElementAttribute("SITE_NODES", typeof(getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITESSITE_NODES))]
        public object[] SITE_NAME
        
            get
            
                return this.sITE_NAMEField;
            
            set
            
                this.sITE_NAMEField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITESSITE_NODES
    

        private string[] nODE_NAMEField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("NODE_NAME")]
        public string[] NODE_NAME
        
            get
            
                return this.nODE_NAMEField;
            
            set
            
                this.nODE_NAMEField = value;
            
        
    

SOAP UI 中的错误如下所示

【问题讨论】:

评论不用于扩展讨论;这个对话是moved to chat。 【参考方案1】:

你为什么不使用两个属性而不是一个?

   public partial class getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITES
    

        private object[] itemNamesField;
        private object[] itemNodessField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("SITE_NAME", typeof(string))]
        public object[] ItemNames
        
            get
            
                return this.itemNamesField;
            
            set
            
                this.itemNamesField = value;
            
        

        [System.Xml.Serialization.XmlElementAttribute("SITE_NODES", typeof(getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITESSITE_NODES))]
        public object[] ItemNodes
        
            get
            
                return this.itemNodesField;
            
            set
            
                this.itemNodesField = value;
            
        
    

【讨论】:

非常感谢您的帮助:) 不是我这边。我会尽快删除这个问题。非常感谢您的建议【参考方案2】:

下面是反序列化 xml 的代码。 Linq 的反序列化速度比下面的代码快得多。所以你必须决定是使用你的代码还是下面的代码更好。通常我建议如果你有一个模式(和类)并试图获取所有数据,最好使用序列化。您将结果放入数据表中,然后我推荐使用 xml linq。我只是想知道为什么我建议删除基类。

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

namespace ConsoleApplication1

    class Program
    
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(getCustomerSites));
            getCustomerSites sites = (getCustomerSites)serializer.Deserialize(reader);
        
    
    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class getCustomerSites //: ResponseBase
    

        private getCustomerSitesPARTY_SITE_NODES[] pARTY_SITE_NODESField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("PARTY_SITE_NODES")]
        public getCustomerSitesPARTY_SITE_NODES[] PARTY_SITE_NODES
        
            get
            
                return this.pARTY_SITE_NODESField;
            
            set
            
                this.pARTY_SITE_NODESField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class getCustomerSitesPARTY_SITE_NODES
    

        private uint cBCM_PARTY_IDField;

        private string cBCM_PARTY_NAMEField;

        private getCustomerSitesPARTY_SITE_NODESPARTY_SERVICES[] pARTY_SERVICESField;

        /// <remarks/>
        public uint CBCM_PARTY_ID
        
            get
            
                return this.cBCM_PARTY_IDField;
            
            set
            
                this.cBCM_PARTY_IDField = value;
            
        

        /// <remarks/>
        public string CBCM_PARTY_NAME
        
            get
            
                return this.cBCM_PARTY_NAMEField;
            
            set
            
                this.cBCM_PARTY_NAMEField = value;
            
        

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("PARTY_SERVICES")]
        public getCustomerSitesPARTY_SITE_NODESPARTY_SERVICES[] PARTY_SERVICES
        
            get
            
                return this.pARTY_SERVICESField;
            
            set
            
                this.pARTY_SERVICESField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class getCustomerSitesPARTY_SITE_NODESPARTY_SERVICES
    

        private string sERVICE_NAMEField;

        private getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITES sERVICE_SITESField;

        /// <remarks/>
        public string SERVICE_NAME
        
            get
            
                return this.sERVICE_NAMEField;
            
            set
            
                this.sERVICE_NAMEField = value;
            
        

        /// <remarks/>
        public getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITES SERVICE_SITES
        
            get
            
                return this.sERVICE_SITESField;
            
            set
            
                this.sERVICE_SITESField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    //[KnownType(typeof(getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITESSITE_NODES))]
    public partial class getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITES
    

        private object[] itemsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("SITE_NAME", typeof(string))]
        [System.Xml.Serialization.XmlElementAttribute("SITE_NODES", typeof(getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITESSITE_NODES))]
        public object[] Items
        
            get
            
                return this.itemsField;
            
            set
            
                this.itemsField = value;
            
        
    

    /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class getCustomerSitesPARTY_SITE_NODESPARTY_SERVICESSERVICE_SITESSITE_NODES
    

        private string[] nODE_NAMEField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("NODE_NAME")]
        public string[] NODE_NAME
        
            get
            
                return this.nODE_NAMEField;
            
            set
            
                this.nODE_NAMEField = value;
            
        
    

【讨论】:

版主锁定了主要问题。我看到了你 2 小时前的变化。看起来您找到了架构以及错误在架构中的位置。 非常感谢您的帮助:) 不是我这边。我会尽快删除这个问题。非常感谢您的建议

以上是关于如何淡化xml并通过c#中的wcf服务传递它的主要内容,如果未能解决你的问题,请参考以下文章

如何在 WCF Rest Service 中传递多个参数:C# 中的字符串和流

WCF 客户端中传递的多个参数不起作用(即使包装了 WebMessageBodyStyle)

C# Restful WCF 服务。无法在帖子正文中反序列化 XML

通过C#代码调用WCF服务中的一个方法。不依赖配置文件等。求完整代码!!

如何在 WCF Rest Service 中传递多个参数?

如何通过 Jquery 调用 C# WCF 服务来修复“ERR_ABORTED 400 (Bad Request)”错误?