将字符串响应从webclient转换为数组/列表

时间:2016-01-11 07:53:49

标签: c# xml linq

我从webclient收到以下字符串,我想将其转换为数组或列表或xml文档,以便我可以遍历它。我尝试转换为Linq.XDocument.Parse(strResponse)但失败并出现错误:根级别的数据无效。第1行,第1位。

转换:

String strResponse = Encoding.UTF8.GetString( response );
XDocument xmldoc = XDocument.Parse( strResponse );
上面的

strResponse看起来像:

array(3) {
    [0]=>
      array(5) {
        ["brand"]=>
        string(6) "Abarth"
        ["range"]=>
        string(3) "500"
        ["model"]=>
        string(8) "500 1.4T"
        ["mm_code"]=>
        string(8) "00815150"
        ["intro_date"]=>
        string(10) "2012-01-12"
      }
      [1]=>
      array(5) {
        ["brand"]=>
        string(6) "Abarth"
        ["range"]=>
        string(3) "500"
        ["model"]=>
        string(17) "500 1.4T esseesse"
        ["mm_code"]=>
        string(9) "008151501"
        ["intro_date"]=>
        string(10) "2012-01-12"
      }
      [2]=>
      array(5) {
        ["brand"]=>
        string(6) "Abarth"
        ["range"]=>
        string(3) "500"
        ["model"]=>
        string(9) "500C 1.4T"
        ["mm_code"]=>
        string(8) "00815180"
        ["intro_date"]=>
        string(10) "2012-01-12"
      }
  }

当我尝试将其转换为xml文档时,是否有人可能会看到上述响应为什么会抛出错误?还有更好的建议吗?

提前致谢。

3 个答案:

答案 0 :(得分:0)

这看起来像JSON而不是XML,您可以尝试使用JSON序列化程序

答案 1 :(得分:0)

您的回复是PHP数组 您可以使用Regex或其他逻辑来编写自己的转换器 或者使用某些第三方库,例如Conversive.PHPSerializationLibrary

答案 2 :(得分:0)

这是一个非常基本的解析器,可以处理文本

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            Parse parse = new Parse(FILENAME);
            List<object> results = parse.Recursion();
        }
    }
    public class Parse
    {
        enum State
        {
            NOTHING,
            GET_NAME,
            GET_VALUE
        }
        static StreamReader reader = null;
        public Parse(string filename)
        {
            reader = new StreamReader(filename);

        }
        public List<object> Recursion()
        {
            List<object> results = new List<object>();
            char[] chr = new  char[1];
            State state = State.NOTHING;
            Boolean exit = false;
            Boolean openDoubleQuote = false;
            string name = "";
            string value = "";
            while(reader.Read(chr, 0, 1) == 1)
            {
                switch (state)
                {
                    case State.NOTHING :
                        switch(chr[0])
                        {
                            case '{' :
                                results.Add(Recursion());                     
                                break;
                            case '\"':
                                state = State.GET_NAME;
                                openDoubleQuote = true;
                                break;
                        }
                        break;
                    case State.GET_NAME:
                        switch(chr[0])
                        {
                            case '\"' :

                                if (openDoubleQuote == false)
                                {
                                    openDoubleQuote = true;
                                }
                                else
                                {
                                    state = State.GET_VALUE;
                                    openDoubleQuote = false;
                                }
                                break;

                            case '}' :

                                exit = true;
                                break;

                            default : 

                                if (openDoubleQuote == true)
                                {
                                    name += chr[0];
                                }
                                break;
                        }
                        break;
                    case State.GET_VALUE:
                        switch(chr[0])
                        {
                            case '\"' :

                                if (openDoubleQuote == false)
                                {
                                    openDoubleQuote = true;
                                }
                                else
                                {
                                    KeyValuePair<string, string> pair = new KeyValuePair<string, string>(name, value);
                                    results.Add(pair);

                                    name = "";
                                    value = "";

                                    state = State.GET_NAME;
                                    openDoubleQuote = false;
                                }
                                break;
                            default :
                            if (openDoubleQuote == true)
                            {
                                value += chr[0];
                            }
                            break;
                        }
                        break;

                }
                if (exit) break;
            }

            return results;
        }
    }
}