Parse jagged array in C#
I'm connecting to an external web service that is implemented using Apache Axis and SOAP 1.2. The web service returns a jagged object array like the one below. Looking at the XML the I have xsi:type="soapenc:Array"
What would be the cleanest/best method of parsing this array in C#2 and C#3 respectively? (I'm specifically 开发者_运维知识库interested in C#2 so a C#3 solution would be for interest only.)
- obj object[] {object[][]} -[0] object {object[]} -[0] object {string} -[1] object {string} -[1] object {object[]} -[0] object {string} -[1] object {bool} -[2] object {object[]} -[0] object {string} -[1] object {object[]} -[0] object {object[][]} -[0] object[] -[0] object{string} -[1] object{string)
Not sure on what would be considered the best practice, but this is one way you could do it. Just need to test if the object is an array, if so use its enumerable interface. Recursively check each array item.
_array = new object[3];
_result = new StringBuilder();
//Populate array here
foreach (object item in _array)
{
ParseObject(item);
}
private void ParseObject(object value)
{
if (value.GetType().IsArray)
{
IEnumerable enumerable = value as IEnumerable;
foreach (object item in enumerable)
{
ParseObject(item);
}
}
else
{
_result.Append(value.ToString() + "\n");
}
}
精彩评论