views:

567

answers:

2

Here is my code, see the part that says ???WHAT TO DO HERE??? I am not sure where I write this XML to in order to be able to send it in my POST via the objRequest there...

string project_id = context.Request.QueryString["project"];
string person_id = context.Request.QueryString["person"];
string post_date = context.Request.QueryString["date"];
string post_hours = context.Request.QueryString["hours"];
string case_num = context.Request.QueryString["case"];


HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://company.updatelog.com/projects/" + project_id + "/time_entries.xml");

objRequest.Method = "POST";

objRequest.ContentType = "application/xml";

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("    ");


using (XmlWriter writer = XmlWriter.Create(?????WHAT TO DO HERE????, settings))
{
    writer.WriteStartElement("time-entry");
    writer.WriteElementString("person-id", person_id);
    writer.WriteElementString("date", post_date);
    writer.WriteElementString("hours", post_hours);
    writer.WriteElementString("description", "Worked on Case #" + case_num);
    writer.WriteEndElement();
}


StreamWriter myWriter = null;


try
{

    myWriter = new StreamWriter(objRequest.GetRequestStream());

    myWriter.Write(post_xml);

}

catch (Exception e)
{

    context.Response.Write(e.Message);

}

finally
{

    myWriter.Close();

}
A: 

You can just use an XmlTextWriter which takes a string builder which basically just writes it to memory.

Code:

var buidler = new StringBuilder();
var writer = XmlWriter.Create(builder);
BFree
then how do I put it into the request?
shogun
You should be able to just write streamWriter.Write(builder.ToString());
BFree
+1  A: 

XmlWriter writer = XmlWriter.Create(objRequest.GetRequestStream(), settings);

You need not create a StreamWriter then. The content that you write in XmlWriter will be written to the requestStream.

shahkalpesh
how do I submit the request?
shogun