在 C# 中解析锯齿状数组

发布于 2024-08-19 06:36:27 字数 545 浏览 4 评论 0原文

我正在连接到使用 Apache Axis 和 SOAP 1.2 实现的外部 Web 服务。 Web 服务返回一个锯齿状对象数组,如下所示。查看 XML,我有 xsi:type="soapenc:Array"

分别在 C#2 和 C#3 中解析此数组的最干净/最佳方法是什么? (我对 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烈酒灼喉 2024-08-26 06:36:27

不确定什么被认为是最佳实践,但这是您可以做到的一种方法。只需测试该对象是否是数组,如果是,则使用其可枚举接口。递归检查每个数组项。

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

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");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文