使用XDocument加载字符串时路径中的非法字符

Bou*_*ory 40 c# xml .net-4.0

我在字符串中有非常简单的XML,我正在尝试加载,XDocument以便我可以使用LINQ to XML:

 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
Run Code Online (Sandbox Code Playgroud)

Illegal characters in path.当我尝试加载XML时,我收到一个错误; 有人可以解释为什么会这样吗?谢谢.

Bro*_*ass 128

您正在寻找XDocument.Parse- XDocument.Load适用于不是xml字符串的文件:

var doc = XDocument.Parse(xmlString); 
Run Code Online (Sandbox Code Playgroud)

  • 对于`XmlDocument`它是`XmlDocument.LoadXml(xmlString)` (7认同)

Raj*_*ajN 11

使用

var doc = XDocument.Parse(xmlString); 
Run Code Online (Sandbox Code Playgroud)