0

I am trying to read in POST data to an ASPX (c#) page. I have got the post data now inside a string. I am now wondering if this is the best way to use it. Using the code here (http://stackoverflow.com/questions/10386534/using-request-getbufferlessinputstream-correctly-for-post-data-c-sharp) I have the following string

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

As this is now in a string, I am splitting based on a space.

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

The trouble comes when a value has a space in it. For example, variable3="foo 3" upsets the code.

Is there something better I should be doing to parse the incoming http post variables within the string??

1
  • You can use RegEx to parse your string Commented May 1, 2012 at 11:05

1 Answer 1

2

You might want to treat it as XML directly:

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

Of course you can change that last line to whatever you want, the above is a rough example.

Sign up to request clarification or add additional context in comments.

1 Comment

that's great- thanks! I can see the name -> value in the debug window. I am confused as to how I'd populate my variables though. (using the 'var values = ele...' line). Can I specify to populate string var1 = ele.Attributes().Select() to get just the value of "variable1" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.