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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10586838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:23:47  来源:igfitidea点击:

Illegal characters in path when loading a string with XDocument

c#xml.net-4.0

提问by BoundForGlory

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

我在尝试加载的字符串中有非常简单的 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

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.

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

采纳答案by BrokenGlass

You are looking for XDocument.Parse- XDocument.Loadis for filesnot xml strings:

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

var doc = XDocument.Parse(xmlString); 

回答by RajN

Use

var doc = XDocument.Parse(xmlString); 

回答by Akshay Mishra

Use this for XML String

将此用于 XML 字符串

        XDocument reader;
        using (StringReader s = new StringReader(**XmlResult**))
        {
            reader = XDocument.Load(s);
        }