简体   繁体   English

在C#中解析锯齿状数组

[英]Parse jagged array in C#

I'm connecting to an external web service that is implemented using Apache Axis and SOAP 1.2. 我正在连接到使用Apache Axis和SOAP 1.2实现的外部Web服务。 The web service returns a jagged object array like the one below. Web服务返回一个锯齿状的对象数组,如下所示。 Looking at the XML the I have xsi:type="soapenc:Array" 看看XML我有xsi:type =“soapenc:Array”

What would be the cleanest/best method of parsing this array in C#2 and C#3 respectively? 分别在C#2和C#3中解析这个数组的最干净/最好的方法是什么? (I'm specifically interested in C#2 so a C#3 solution would be for interest only.) (我对C#2特别感兴趣,所以C#3解决方案只会引起兴趣。)

- 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");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM