mynt ropo Ответов: 1

Как реализовать приведенный ниже XML-ответ в ASP.NET код с#


Я не могу получить данные из приведенного ниже ответа. Получение ошибки типа "вложенная таблица 'деноминации', которая наследует свое пространство имен, не может иметь несколько родительских таблиц в разных пространствах имен". Я пытаюсь реализовать в ASP.NET с#.


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soap:Body>
		<GetProductListResult xmlns="http://EDTS.DirectTopUpServices.DataTypes/2006/10">
			<MessageID>810190215</MessageID>
			<Products>
				<Product>
					<CountryID>59</CountryID>
					<CountryISO>LK</CountryISO>
					<CountryCode>LK</CountryCode>
					<CountryName>Sri Lanka</CountryName>
					<OperatorID>48</OperatorID>
					<OperatorCode>DG</OperatorCode>
					<OperatorName>Dialog Sri Lanka</OperatorName>
					<CustomerCareNumber>+94-77767-8678</CustomerCareNumber>
					<PhoneNumberMask>+94-xxxxx-xxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				<Product>
					<CountryID>60</CountryID>
					<CountryISO>PH</CountryISO>
					<CountryCode>PH</CountryCode>
					<CountryName>Philippines</CountryName>
					<OperatorID>54</OperatorID>
					<OperatorCode>TM</OperatorCode>
					<OperatorName>Touch Mobile Philippines</OperatorName>
					<CustomerCareNumber>+63-2370-1000</CustomerCareNumber>
					<PhoneNumberMask>+63-xxxxx-xxxxx </PhoneNumberMask>
					<Denominations>
						<NumberOfDenominations>5</NumberOfDenominations>
						<Denominations>
							<double>6</double>
							<double>16</double>
							<double>31</double>
							<double>51</double>
							<double>102</double>
						</Denominations>
					</Denominations>
				</Product>
				<Product>
					<CountryID>59</CountryID>
					<CountryISO>LK</CountryISO>
					<CountryCode>LK</CountryCode>
					<CountryName>Sri Lanka</CountryName>
					<OperatorID>58</OperatorID>
					<OperatorCode>TI</OperatorCode>
					<OperatorName>Etisalat Sri Lanka</OperatorName>
					<CustomerCareNumber>+ (94) 72212-3123 </CustomerCareNumber>
					<PhoneNumberMask>+94-xxxxx-xxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				<Product>
					<CountryID>55</CountryID>
					<CountryISO>PK</CountryISO>
					<CountryCode>PK</CountryCode>
					<CountryName>Pakistan</CountryName>
					<OperatorID>80</OperatorID>
					<OperatorCode>WP</OperatorCode>
					<OperatorName>Warid Pakistan</OperatorName>
					<CustomerCareNumber>+92-32184-34357</CustomerCareNumber>
					<PhoneNumberMask>+92-xxxxx-xxxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				<Product>
					<CountryID>55</CountryID>
					<CountryISO>PK</CountryISO>
					<CountryCode>PK</CountryCode>
					<CountryName>Pakistan</CountryName>
					<OperatorID>89</OperatorID>
					<OperatorCode>UF</OperatorCode>
					<OperatorName>Ufone Pakistan</OperatorName>
					<CustomerCareNumber>+92-51111-333100</CustomerCareNumber>
					<PhoneNumberMask>+92-xxxxx-xxxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				
			</Products>
		</GetProductListResult>
	</soap:Body>
</soap:Envelope>


Что я уже пробовал:

sResp.XmlResponse = xDoc.InnerXml.ToString();
//Parse the response xml from ezetop.
StringReader sRead = new StringReader(xDoc.InnerXml.ToString());
dsResp.ReadXml(sRead);

if (dsResp != null)
{
    sResp.Result_Flag = Res_Code.SuccessFlag;
    for (int i = 0; i < dsResp.Tables["Product"].Rows.Count; i++)
    {
        sResp.CountryID = dsResp.Tables["Product"].Rows[i]["CountryID"].ToString().Trim();
        sResp.CountryISO = dsResp.Tables["Product"].Rows[i]["CountryISO"].ToString().Trim();

        for (int j = 0; j < dsResp.Tables["MinMaxValueRange"].Rows.Count; j++)
        {
            sResp.MinValue = dsResp.Tables["MinMaxValueRange"].Rows[i]["MinValue"].ToString().Trim();
            sResp.MaxValue = dsResp.Tables["MinMaxValueRange"].Rows[i]["MaxValue"].ToString().Trim();
        }
        for (int k = 0; k < dsResp.Tables["Denominations"].Rows.Count; k++)
        {
            int count = 0;
            sResp.NumberOfDenominations = dsResp.Tables["Denominations"].Rows[i]["NumberOfDenominations"].ToString().Trim();
            count = Convert.ToInt32(sResp.NumberOfDenominations);
            for (int l = 0; l < count; l++)
            {
                sResp.Denom_double = dsResp.Tables[11].Rows[i]["double"].ToString().Trim();
            }
        }

    }

Maciej Los

"Я не могу получить данные...- ... совсем не описательно. Можете ли вы быть более конкретным и предоставить подробную информацию, например, какой результат вы ожидаете...

1 Ответов

Рейтинг:
0

dnxit

Почему бы вам не сериализовать его вместо того, чтобы делать это вручную ?

С вашими данными я только что сделал консольную программу, попробуйте

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		var result = Serializer.Deserialize();
		foreach(var item in result.Body.GetProductListResult.Products.Product)
		{
			Console.WriteLine("{0}", item.CountryCode);
			Console.WriteLine("{0}", item.CountryName);
			Console.WriteLine("{0}", item.CustomerCareNumber);
		}
	}
}

public static class Serializer
{
	public static Envelope Deserialize()
	{
		var type = typeof (Envelope);

		var serializer = new XmlSerializer(type);

		var xmlString = @"<?xml version=""1.0"" encoding=""utf-8""?>
		<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
		<soap:Body>
		<GetProductListResult xmlns=""http://EDTS.DirectTopUpServices.DataTypes/2006/10"">
			<MessageID>810190215</MessageID>
			<Products>
				<Product>
					<CountryID>59</CountryID>
					<CountryISO>LK</CountryISO>
					<CountryCode>LK</CountryCode>
					<CountryName>Sri Lanka</CountryName>
					<OperatorID>48</OperatorID>
					<OperatorCode>DG</OperatorCode>
					<OperatorName>Dialog Sri Lanka</OperatorName>
					<CustomerCareNumber>+94-77767-8678</CustomerCareNumber>
					<PhoneNumberMask>+94-xxxxx-xxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				<Product>
					<CountryID>60</CountryID>
					<CountryISO>PH</CountryISO>
					<CountryCode>PH</CountryCode>
					<CountryName>Philippines</CountryName>
					<OperatorID>54</OperatorID>
					<OperatorCode>TM</OperatorCode>
					<OperatorName>Touch Mobile Philippines</OperatorName>
					<CustomerCareNumber>+63-2370-1000</CustomerCareNumber>
					<PhoneNumberMask>+63-xxxxx-xxxxx </PhoneNumberMask>
					<Denominations>
						<NumberOfDenominations>5</NumberOfDenominations>
						<Denominations>
							<double>6</double>
							<double>16</double>
							<double>31</double>
							<double>51</double>
							<double>102</double>
						</Denominations>
					</Denominations>
				</Product>
				<Product>
					<CountryID>59</CountryID>
					<CountryISO>LK</CountryISO>
					<CountryCode>LK</CountryCode>
					<CountryName>Sri Lanka</CountryName>
					<OperatorID>58</OperatorID>
					<OperatorCode>TI</OperatorCode>
					<OperatorName>Etisalat Sri Lanka</OperatorName>
					<CustomerCareNumber>+ (94) 72212-3123 </CustomerCareNumber>
					<PhoneNumberMask>+94-xxxxx-xxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				<Product>
					<CountryID>55</CountryID>
					<CountryISO>PK</CountryISO>
					<CountryCode>PK</CountryCode>
					<CountryName>Pakistan</CountryName>
					<OperatorID>80</OperatorID>
					<OperatorCode>WP</OperatorCode>
					<OperatorName>Warid Pakistan</OperatorName>
					<CustomerCareNumber>+92-32184-34357</CustomerCareNumber>
					<PhoneNumberMask>+92-xxxxx-xxxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				<Product>
					<CountryID>55</CountryID>
					<CountryISO>PK</CountryISO>
					<CountryCode>PK</CountryCode>
					<CountryName>Pakistan</CountryName>
					<OperatorID>89</OperatorID>
					<OperatorCode>UF</OperatorCode>
					<OperatorName>Ufone Pakistan</OperatorName>
					<CustomerCareNumber>+92-51111-333100</CustomerCareNumber>
					<PhoneNumberMask>+92-xxxxx-xxxxx </PhoneNumberMask>
					<MinMaxValueRange>
						<MinValue>5</MinValue>
						<MaxValue>250</MaxValue>
					</MinMaxValueRange>
				</Product>
				
			</Products>
		</GetProductListResult>
		</soap:Body>
		</soap:Envelope>";

		using (var stringReader = new StringReader(xmlString))
		{
			return serializer.Deserialize(stringReader) as Envelope;
		}
	}
}


	[XmlRoot(ElementName="MinMaxValueRange", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
	public class MinMaxValueRange {
		[XmlElement(ElementName="MinValue", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string MinValue { get; set; }
		[XmlElement(ElementName="MaxValue", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string MaxValue { get; set; }
	}

	[XmlRoot(ElementName="Product", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
	public class Product {
		[XmlElement(ElementName="CountryID", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string CountryID { get; set; }
		[XmlElement(ElementName="CountryISO", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string CountryISO { get; set; }
		[XmlElement(ElementName="CountryCode", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string CountryCode { get; set; }
		[XmlElement(ElementName="CountryName", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string CountryName { get; set; }
		[XmlElement(ElementName="OperatorID", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string OperatorID { get; set; }
		[XmlElement(ElementName="OperatorCode", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string OperatorCode { get; set; }
		[XmlElement(ElementName="OperatorName", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string OperatorName { get; set; }
		[XmlElement(ElementName="CustomerCareNumber", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string CustomerCareNumber { get; set; }
		[XmlElement(ElementName="PhoneNumberMask", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string PhoneNumberMask { get; set; }
		[XmlElement(ElementName="MinMaxValueRange", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public MinMaxValueRange MinMaxValueRange { get; set; }
		[XmlElement(ElementName="Denominations", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public Denominations Denominations { get; set; }
	}

	[XmlRoot(ElementName="Denominations", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
	public class Denominations {
		[XmlElement(ElementName="double", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public List<string> Double { get; set; }
	}

	[XmlRoot(ElementName="Products", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
	public class Products {
		[XmlElement(ElementName="Product", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public List<Product> Product { get; set; }
	}

	[XmlRoot(ElementName="GetProductListResult", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
	public class GetProductListResult {
		[XmlElement(ElementName="MessageID", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public string MessageID { get; set; }
		[XmlElement(ElementName="Products", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public Products Products { get; set; }
		[XmlAttribute(AttributeName="xmlns")]
		public string Xmlns { get; set; }
	}

	[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
	public class Body {
		[XmlElement(ElementName="GetProductListResult", Namespace="http://EDTS.DirectTopUpServices.DataTypes/2006/10")]
		public GetProductListResult GetProductListResult { get; set; }
	}

	[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
	public class Envelope {
		[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
		public Body Body { get; set; }
		[XmlAttribute(AttributeName="soap", Namespace="http://www.w3.org/2000/xmlns/")]
		public string Soap { get; set; }
		[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
		public string Xsi { get; set; }
		[XmlAttribute(AttributeName="xsd", Namespace="http://www.w3.org/2000/xmlns/")]
		public string Xsd { get; set; }
	}