在 ASP.NET 中,在哪里使用 XmlWriter 编写 XML 以通过 HttpWebRequest POST 发送?

发布于 2024-07-17 10:24:49 字数 1366 浏览 6 评论 0原文

这是我的代码,请参阅“???在这里做什么???”的部分 我不确定我将该 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猛虎独行 2024-07-24 10:24:49

<代码>
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.

蓝海 2024-07-24 10:24:49

您可以只使用 XmlTextWriter,它采用字符串生成器,基本上只是将其写入内存。

代码:

var buidler = new StringBuilder();
var writer = XmlWriter.Create(builder);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文