Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从http post/string c中提取变量和值#_C#_Asp.net_.net 4.0 - Fatal编程技术网

C# 从http post/string c中提取变量和值#

C# 从http post/string c中提取变量和值#,c#,asp.net,.net-4.0,C#,Asp.net,.net 4.0,我正在尝试将POST数据读入ASPX(c#)页面。我现在已经在一个字符串中获得了post数据。我现在想知道这是不是最好的使用方法。在这里使用代码(http://stackoverflow.com/questions/10386534/using-request-getbufferlessinputstream-correctly-for-post-data-c-sharp)我有以下字符串 <callback variable1="foo1" variable2="foo2" variabl

我正在尝试将POST数据读入ASPX(c#)页面。我现在已经在一个字符串中获得了post数据。我现在想知道这是不是最好的使用方法。在这里使用代码(http://stackoverflow.com/questions/10386534/using-request-getbufferlessinputstream-correctly-for-post-data-c-sharp)我有以下字符串

<callback variable1="foo1" variable2="foo2" variable3="foo3" />

因为这是一个字符串,所以我基于一个空格进行拆分

    string[] pairs = theResponse.Split(' ');
    Dictionary<string, string> results = new Dictionary<string, string>();
    foreach (string pair in pairs)
    {
        string[] paramvalue = pair.Split('=');
        results.Add(paramvalue[0], paramvalue[1]);
        Debug.WriteLine(paramvalue[0].ToString());
    }
string[]pairs=响应拆分(“”);
字典结果=新字典();
foreach(成对的字符串对)
{
字符串[]paramvalue=pair.Split('=');
结果。添加(paramvalue[0],paramvalue[1]);
Debug.WriteLine(paramvalue[0].ToString());
}
当一个值中有一个空格时,问题就来了。例如,
variable3=“foo3”
会扰乱代码


我是否应该做些更好的事情来解析字符串中传入的http post变量

您可能希望直接将其视为XML:

// just use 'theResponse' here instead
var xml = "<callback variable1=\"foo1\" variable2=\"foo2\" variable3=\"foo3\" />";

// once inside an XElement you can get all the values
var ele = XElement.Parse(xml);

// an example of getting the attributes out
var values = ele.Attributes().Select(att => new { Name = att.Name, Value = att.Value });

// or print them
foreach (var attr in ele.Attributes())
{
    Console.WriteLine("{0} - {1}", attr.Name, attr.Value);
}
//只需在此处使用“theResponse”
var xml=“”;
//一旦进入XElement,就可以获得所有值
var ele=XElement.Parse(xml);
//获取属性的示例
var values=ele.Attributes().Select(att=>new{Name=att.Name,Value=att.Value});
//或者打印出来
foreach(ele.Attributes()中的var attr)
{
WriteLine(“{0}-{1}”,attr.Name,attr.Value);
}

当然,您可以将最后一行更改为您想要的任何内容,以上只是一个粗略的示例。

太好了-谢谢!我可以在调试窗口中看到名称->值。但我对如何填充变量感到困惑。(使用'var values=ele…'行)。我可以指定填充字符串var1=ele.Attributes().Select()以仅获取“variable1”的值吗?您可以使用正则表达式来解析字符串