c# - 将xml复制到没有标题的新文件
问题描述
我正在尝试获取已发送到我的应用程序的 Xml 文件的正文数据。
但是当我复制内容并将其放入新文件时,内容不正确。
原始 XML:
<?xml version="1.0" encoding="utf-8"?>
<eM_NM_001 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MESSAGE>
....
</MESSAGE>
</eM_NM_001>
粘贴正文后的新文件:
-----------------------8d618ce02a1a097
Content-Disposition: form-data; name="file"; filename="14_20180912162639.xml"
Content-Type: application/octet-stream
<?xml version="1.0" encoding="utf-8"?>
<eM_NM_001 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MESSAGE>
...
</MESSAGE>
</eM_NM_001>
-----------------------8d618ce02a1a097--
我只希望第二个文件也包含xml
而不是标题数据......(至少我认为它是标题数据)
我到底该如何解决这个问题?
到目前为止我得到了什么:
//POST api/<controller>
[HttpPost]
public string Post()
{
var result = "";
if (Request.Content != null)
{
string map = @"D:\bestelApp\bestelApp\Content\bestellingen\ontvangen";
string count = Directory.GetFiles(map, "*.xml").Count().ToString();
string extention = ".xml";
string fileName = "bestelling" + (count != "0" ? count : "") + extention;
string path = map + "//" + fileName;
using (Stream output = File.OpenWrite(path))
{
using (Stream input = HttpContext.Current.Request.GetBufferedInputStream())
{
input.CopyTo(output);
}
}
result = new bestellingenController().ConvertXmlToObj(fileName, path);
}
return result;
}
到目前为止我已经尝试过:
[HttpPost]
public void Post(HttpRequestMessage request) {
var doc = new XmlDocument();
doc.Load(request.Content.ReadAsStreamAsync().Result);
doc.Save(@"D:\bestelApp\bestelApp\Content\bestellingen\ontvangen");
}
var context = new HttpContextWrapper(HttpContext.Current);
context.Request.ContentType = null;
HttpRequestBase request = context.Request; //This was an attempt to set everything to null except the xml.
//Get the data from the HTTP stream
var body = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(body);
xmlDocument.Save(@"D:\bestelApp\bestelApp\Content\bestellingen\ontvangen\" + fileName);
解决方案
推荐阅读
- visual-studio - JetBrains Rider 中 Visual Studio 的“无需调试即可启动”?
- reactjs - 每次使用 ReactJS 渲染时,如何将流畅的 UI 模式滚动到顶部
- python - 使用 tfidf 作为特征
- azure-devops - Azure DevOps dotnet build 找不到恢复的 nuget 包
- python-2.7 - valueerror: 生成 csv 文件并在 django 中保存到模型时对已关闭文件的 i/o 操作
- php - 小于符号 (<) 之后的内容将在脚本标记中删除
- react-native - React Native 个性化原生广告
- asp.net-core-mvc - 我可以将 ImageSharp 与 .Net Core 一起使用来截取网络浏览器的屏幕截图吗
- java - 将 SVG 嵌入到 Batik SVG 中
- winapi - 如何将字符串变量而不是文件名参数传递给 TIDY?