将xml复制到没有标题的新文件

时间:2018-09-12 14:53:43

标签: c# xml asp.net-mvc file request

我正在尝试获取已发送到我的应用程序的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而不是Header数据...(至少我认为这是header数据)

到底该如何解决?


到目前为止我所拥有的:

        //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);

I had already asked this question but poorly..

0 个答案:

没有答案