2010-09-28 46 views
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的错误?

在此先感谢您的帮助。

+0

你是否通过调试器走过你的代码 – 2010-09-28 12:54:15

+0

我刚发现一个无效的跨线程错误,但我似乎无法再找到它... – JBarnes 2010-09-28 13:04:08

+0

我已经得到它的工作。没有想法为什么。我认为Cross Thread Error与messageBox是UI的一部分有关,所以我删除了它,只是添加了一些新的xmlwriter的东西......并且它工作。我以前试过这个,但它不起作用,所以我会继续关注它,但迄今为止这么好。仍然困惑,但从来没有想过。 – JBarnes 2010-09-28 13:21:17

回答

0

我有它的工作。没有想法为什么。我认为Cross Thread Error与messageBox是UI的一部分有关,所以我删除了它,只是添加了一些新的xmlwriter的东西......并且它工作。我以前试过这个,但它不起作用,所以我会继续关注它,但迄今为止这么好。仍然困惑,但从来没有想过。