首页 > 解决方案 > 将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);

我已经问过这个问题但很糟糕..

标签: c#xmlasp.net-mvcfilerequest

解决方案


推荐阅读