views:

19

answers:

1

I have a method that generates XML for a class I have created -getXML(). This is called from the following methods:

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

The getXML itself is as follows:

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

(Sorry about the excess amounts of code).

This all works fine, and I can see the xml output. But now I want to add some more code to the getXML() function, and Silverlight just won't let me do it. Even a simple MessageBox.Show("something") won't display, and the whole method now fails to generate any XML. Having said that, there are no errors or warnings generated within Visual Studio.

I've added new code in other areas of the same .cs file and it all works fine, it is just in this one method. I've tried rebuilds but also to no avail, so I'm confused as to how this could be the case. Is it a Visual Studio bug?

Thanks in advance for any help.

A: 

I've got it working. No ideas why. I think the Cross Thread Error was to do with messageBox being part of the UI, so I removed that and just added in some new xmlwriter stuff... and it worked. I had tried this before and it didn't work so I'll keep my eye on it but so far so good. Still puzzled, but nevermind.

JBarnes