无法在XmlDocument中加载字符串

时间:2019-08-12 12:33:18

标签: asp.net xml asp.net-core

我正在使用ASP.Net Core 2.2。我正在从返回XML结果的API获取数据。但是,当MyController要加载text变量时,它将因该错误而中断

IOException: The filename, directory name, or volume label syntax is incorrect : 'I:\Web Development\tamrin\Pizza\Pizza\<ProductType xmlns:i="http:\www.w3.org\2001\XMLSchema-instance" xmlns="http:\schemas.datacontract.org\2004\07\MyApi.Core.Models"><Id>2<\Id><Image>\images\burger.png<\Image><Name>Hamburger<\Name><\ProductType>'

这里是 MyController

public async Task<IActionResult> Index()
    { 
        using (var httpClient = new HttpClient())
        {
            using (var response = await httpClient.GetAsync("https://localhost:44374/api/productTypes/2"))
            {
                string text = await response.Content.ReadAsStringAsync();
                XmlDocument doc = new XmlDocument();

                doc.Load(text);      //app breaks here

                doc.Deserialize<ProductType>();
            }
        }
        return View("index");
    }

这是text的内容,它是doc.load(text)方法的参数。(在调试模式下)

"<ProductType xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MyApi.Core.Models\"><Id>2</Id><Image>/images/burger.png</Image><Name>Hamburger</Name></ProductType>"

这是我在MYController中使用的 Deserilize 扩展方法(我认为这是不必要的,因为应用程序在上台之前就已经崩溃了)

public static class XmlDeserilizer
{
    public static T Deserialize<T>(this XmlDocument document)
        where T : class
    {
        XmlReader reader = new XmlNodeReader(document);
        var serializer = new XmlSerializer(typeof(T));
        T result = (T)serializer.Deserialize(reader);
        return result;
    }
}

如果您需要更多信息,请在评论中让我知道。

1 个答案:

答案 0 :(得分:3)

flutter create Project_name 需要一个文件名。 您需要使用Load()