我有一个为我创建的类-getXML()生成XML的方法。这可以通过以下方法调用:
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的错误吗?
提前感谢您的帮助。
答案 0 :(得分:0)
我已经开始工作了。没有想法为什么。我认为Cross Thread Error与messageBox是UI的一部分有关,所以我删除了它,只是添加了一些新的xmlwriter东西......而且它有效。我之前尝试过这个并没有用,所以我会继续关注它,但到目前为止还是那么好。仍然困惑,但没关系。