将控制台应用程序输出获取到 xml 文件并保存文件 [重复]
Posted
技术标签:
【中文标题】将控制台应用程序输出获取到 xml 文件并保存文件 [重复]【英文标题】:Get Console application output to xml file and save the file [duplicate] 【发布时间】:2021-08-05 01:01:02 【问题描述】:我的 ASP.Net 控制台应用程序生成一个 xml 输出,我如何创建一个 xml 文件并保存它?
namespace ConsoleApp2
课堂节目 静态异步任务 Main(string[] args) var TicketTask = 等待 createTicket(); var TicketTask2 = 等待 createTicket2();
async static Task<string> createTicket2()
var content = "unknown error";
using (var httpClient = new System.Net.Http.HttpClient())
using (var request = new System.Net.Http.HttpRequestMessage(new HttpMethod("POST"), "http://10/sap-lve/"))
try
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("an:s"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic base64authorization");
String str1 = @"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><Get_api_version xmlns='http://e.s.a.com'/></s:Body></s:Envelope>";
request.Content = new StringContent(str1, Encoding.UTF8, "text/xml"); ;
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);
content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
//Console.WriteLine(response);
var result = response.Content.ReadAsStringAsync();
Console.WriteLine(result.Result);
catch (Exception ex)
content = ex.Message;
return content;
【问题讨论】:
【参考方案1】:从 xml 字符串创建一个 XmlDocument 并使用其保存方法。
string s = "<xml><sample></sample></xml>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
doc.Save("myfilename.xml");
【讨论】:
谢谢,解决了!以上是关于将控制台应用程序输出获取到 xml 文件并保存文件 [重复]的主要内容,如果未能解决你的问题,请参考以下文章