0
我想對一個對象進行反序列化。我看到msdn.com下面的代碼:從服務器反序列化XML響應
private void DeserializeObject(string filename)
{
Debug.WriteLine("Reading with XmlReader");
// Create an instance of the XmlSerializer specifying type and namespace.
XmlSerializer serializer = new XmlSerializer(typeof(User));
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(filename);
// Declare an object variable of the type to be deserialized.
User i;
// Use the Deserialize method to restore the object's state.
i = (User)serializer.Deserialize(reader);
fs.Close();
// Write out the properties of the object.
Debug.WriteLine(
i.field1+ "\t" +
i.field2+ "\t" +
i.field3+ "\t" +
i.field4);
}
不過,我不想deserialise一個文件,而是一個XML流我從服務器的響應得到的,這裏對應的代碼所示:
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
var Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
if (Response == "")
{
//show some error msg to the user
}
else
{
//Your response will be available in "Response"
string mystring = Response.ToString();
//Mytext.Text = mystring;
Debug.WriteLine(mystring);
DeserializeObject("myxml"); <---- call deserialise
}
我該如何做到這一點?我使用xsd.exe工具根據XML創建了一個名爲「User」的類。