如何将IFormFile (xml文件)解析为XML对象?要对其进行反序列化,我需要将xml作为字符串。如何从IFormFile创建xml字符串?
[HttpPost]
public async Task<MyResponse> Upload([FromForm] IFormFile xmlFile)
{
using (var reader = new StreamReader(file.OpenReadStream()))
{
}
...
}
我的XML模型:
[Serializable()]
[XmlType("ProductLines")]
public class ProductLinesExtentionResult
{
[XmlElement(ElementName = "ProductLine")]
public List<ProductLine> ProductLine { get; set; }
}
[XmlRoot(ElementName = "ProductLine")]
public class ProductLine
{
[XmlElement(ElementName = "Components")]
public List<Component> Components { get; set; }
}
[XmlRoot(ElementName = "Component")]
public class Component
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Component")]
public List<Component> Components { get; set; }
}
发布于 2021-09-06 09:57:08
您可以不使用string
,只需将IFormFile
的stream
直接传递给XmlSerializer
的Deserialize
方法即可。
[HttpPost]
public async Task<MyResponse> Upload([FromForm] IFormFile xmlFile)
{
var serializer = new XmlSerializer(typeof(ProductLinesExtentionResult));
var productLinesExtentionResult = serializer.Deserialize(xmlFile.OpenReadStream());
// ...
}
https://stackoverflow.com/questions/69078538
复制相似问题