XML序列化以返回String数组

时间:2019-04-30 07:03:50

标签: c# asp.net xml soap webforms

我当前的XML响应如下所示(仅显示了重要部分)。我添加了服务引用,这些服务引用为我生成了一些代理类,不幸的是它们没有按需返回输出。下面的c#代码仅返回最后一个“值”数据。我最终想要的是拥有将在gridview中显示的“值”数组。

<return>
<fields></fields>
<fields></fields>
<fields></fields>
<values> 
   <value></value>
   <value></value>
   <value></value>
</values>
<values> 
   <value></value>
   <value></value>
   <value></value>
</values>
//proxy class 
 /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://ws.server.mhaccess.crimsonlogic.com/")]

    public partial class qwsOutput : baseWebServiceOutput {

        private string[] fieldsField;


        public List<string> values { get; set; }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("fields", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=0)]
        public string[] fields {
            get {
                return this.fieldsField;
            }
            set {
                this.fieldsField = value;
                this.RaisePropertyChanged("fields");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true, Order = 1)]
        [System.Xml.Serialization.XmlArrayItemAttribute("value", typeof(string[]), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]

        public List<string> value
        {
            get
            {
                return this.values;
            }
            set
            {
                this.values = value;
                this.RaisePropertyChanged("values");
            }
        }


    }

//code use to call soap request
SoapService.queryWebServiceSoapPortClient client = new 
SoapService.queryWebServiceSoapPortClient();
client.ClientCredentials.UserName.UserName = userID;
client.ClientCredentials.UserName.Password = Password;

SoapService.qwsInput query1 = new SoapService.qwsInput();
SoapService.queryResponse response = new 
SoapService.queryResponse();

query1.userId = userID;
query1.pass = Password;
query1.queryId = queryId;
query1.qwsInputParams = InputParam;

query a = new query();

a.queryInput = query1;

try
{
    response = client.query(a);
}
catch (Exception error)
{

    var b = error.ToString();
}

//更新的代码

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://ws.server.mhaccess.crimsonlogic.com/")]
public partial class qwsOutput : baseWebServiceOutput {

    private string[] fieldsField;

    //private string[][] valuesField;

    private object[] valuesField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("fields", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=0)]
    public string[] fields {
        get {
            return this.fieldsField;
        }
        set {
            this.fieldsField = value;
            this.RaisePropertyChanged("fields");
        }
    }

    ///// <remarks/>
    //[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=1)]
    //[System.Xml.Serialization.XmlArrayItemAttribute("values", typeof(returnValues), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("values", typeof(returnValues), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]

    public object[] values {
        get {
            return this.valuesField;
        }
        set {
            this.valuesField = value;
            //this.RaisePropertyChanged("values");
        }
    }
}






/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class returnValues
{

    private returnValuesValue[] valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("value", Form = 
    System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public returnValuesValue[] value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}



/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class returnValuesValue
{

}

1 个答案:

答案 0 :(得分:1)

我测试了以下代码,并且可以正常工作:

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

namespace ConsoleApplication110
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader reader = new StringReader(xml);
            XmlSerializer serializer = new XmlSerializer(typeof(Return));
            Return _return = (Return)serializer.Deserialize(reader);

        }
    }
    [XmlRoot("return")]
    public class Return
    {
        [XmlElement("fields")]
        public List<string> fields { get; set; }
        [XmlElement("values")]
        public List<Value> values { get; set; }
    }
    [XmlRoot("value")]
    public class Value
    {
        [XmlElement("value")]
        public List<string> value { get; set; }
    }

}