0
我有一個方法可以爲我創建的類生成XML -getXML()。這是從下面的方法調用:奇怪的Silverlight問題
private void send_Response()
{
String xmlUrl = ((App)Application.Current).Resources["xmlUrl"].ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(xmlUrl, UriKind.Absolute));
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}
void RequestReady(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
Stream stream = request.EndGetRequestStream(asyncResult);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("assessmentXml=" + assessment.getXML(testComplete));
writer.Flush();
writer.Close();
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
}
void ResponseReady(IAsyncResult asyncResult)
{
HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
Stream s = response.GetResponseStream();
StreamReader sr = new StreamReader(s);
String line = "";
String Result = "";
line = sr.ReadLine();
while (line != null)
{
Result += line;
line = sr.ReadLine();
}
s.Close();
}
的的getXML本身如下:
public String getXML(bool end)
{
StringWriter output = new StringWriterWithEncoding(Encoding.UTF8);
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
ws.Encoding = new UTF8Encoding(true);
ws.ConformanceLevel = ConformanceLevel.Document;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
writer.WriteStartElement("root");
if (end)
{
writer.WriteAttributeString("testComplete", "true");
}
else
{
writer.WriteAttributeString("testComplete", "false");
}
for (int i = 0; i < theSections.Count(); i++)
{
writer.WriteStartElement("section");
writer.WriteAttributeString("id", theSections.ElementAt(i).Id.ToString());
writer.WriteAttributeString("title", theSections.ElementAt(i).Name.ToString());
writer.WriteAttributeString("completed", theSections.ElementAt(i).Completed.ToString());
for (int j = 0; j < theSections.ElementAt(i).Elements.Count(); j++)
{
writer.WriteStartElement(theSections.ElementAt(i).Elements.ElementAt(j).Name.ToString());
for (int a = 0; a < theSections.ElementAt(i).Elements.ElementAt(j).Attributes().Count(); a++)
{
writer.WriteAttributeString(theSections.ElementAt(i).Elements.ElementAt(j).Attributes().ElementAt(a).Name.ToString(), theSections.ElementAt(i).Elements.ElementAt(j).Attributes().ElementAt(a).Value);
}
for (int c = 0; c < theSections.ElementAt(i).Elements.ElementAt(j).Elements().Count(); c++)
{
writer.WriteStartElement(theSections.ElementAt(i).Elements.ElementAt(j).Elements().ElementAt(c).Name.ToString());
writer.WriteString(theSections.ElementAt(i).Elements.ElementAt(j).Elements().ElementAt(c).Value);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
writer.WriteEndElement();
}
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
return output.ToString();
}
(很抱歉的過量的代碼)。
這一切工作正常,我可以看到xml輸出。但是現在我想向getXML()函數添加更多代碼,並且Silverlight不會讓我這樣做。即使是一個簡單的MessageBox.Show(「something」)也不會顯示,整個方法現在無法生成任何XML。話雖如此,Visual Studio中沒有生成錯誤或警告。
我已經在同一個.cs文件的其他區域添加了新的代碼,它一切正常,只是在這一個方法中。我試過重建,但也無濟於事,所以我很困惑,這可能是怎麼回事。它是一個Visual Studio的錯誤?
在此先感謝您的幫助。
你是否通過調試器走過你的代碼 – 2010-09-28 12:54:15
我剛發現一個無效的跨線程錯誤,但我似乎無法再找到它... – JBarnes 2010-09-28 13:04:08
我已經得到它的工作。沒有想法爲什麼。我認爲Cross Thread Error與messageBox是UI的一部分有關,所以我刪除了它,只是添加了一些新的xmlwriter的東西......並且它工作。我以前試過這個,但它不起作用,所以我會繼續關注它,但迄今爲止這麼好。仍然困惑,但從來沒有想過。 – JBarnes 2010-09-28 13:21:17