加载一个字符串的XDocument时路径中具有非法字符字符串、路径、字符、加载

2023-09-09 21:08:18 作者:噬血

我在我试图通过的XDocument 来加载,这样我可以使用LINQ到SQL字符串很简单的XML:

I have very simple XML in a string that I'm trying to load via XDocument so that I can use LINQ to SQL:

 var xmlString = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
 <person>Test Person</person>";

 var doc = XDocument.Load(xmlString); //'Illegal characters in path' error thrown here

我得到一个非法字符的路径错误抛出,当我尝试加载XML。可能有人请解释为什么发生这种情况?谢谢你。

I get an Illegal characters in path. error thrown when I try to load the XML; could someone please explain why this is happening? Thanks.

推荐答案

您正在寻找 XDocument.Parse - XDocument.Load 是文件的不是XML字符串:

You are looking for XDocument.Parse - XDocument.Load is for files not xml strings:

var doc = XDocument.Parse(xmlString);