在 ASP.NET 中,在哪里使用 XmlWriter 编写 XML 以通过 HttpWebRequest POST 发送?
这是我的代码,请参阅“???在这里做什么???”的部分 我不确定我将该 XML 写入何处,以便能够通过那里的 objRequest 在我的 POST 中发送它...
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
<代码>
XmlWriter writer = XmlWriter.Create(objRequest.GetRequestStream(), settings);
那么您不需要创建 StreamWriter。 您在 XmlWriter 中写入的内容将写入 requestStream。
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.
您可以只使用 XmlTextWriter,它采用字符串生成器,基本上只是将其写入内存。
代码:
You can just use an XmlTextWriter which takes a string builder which basically just writes it to memory.
Code: