C# 从字符串填充 XDocument
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/747554/
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
Populate XDocument from String
提问by StevenMcD
I'm working on a little something and I am trying to figure out whether I can load an XDocument from a string. XDocument.Load()
seems to take the string passed to it as a path to a physical XML file.
我正在做一些事情,我想弄清楚是否可以从字符串加载 XDocument。XDocument.Load()
似乎将传递给它的字符串作为物理 XML 文件的路径。
I want to try and bypass the step of first having to create the physical XML file and jump straight to populating the XDocument.
我想尝试绕过首先必须创建物理 XML 文件并直接跳转到填充 XDocument 的步骤。
Any ideas?
有任何想法吗?
采纳答案by Ronald Wildenberg
You can use XDocument.Parse
for this.
您可以XDocument.Parse
为此使用。
回答by Samuel
You can use XDocument.Parse(string)
instead of Load(string)
.
您可以使用XDocument.Parse(string)
代替Load(string)
.
回答by Martin Peck
How about this...?
这个怎么样...?
TextReader tr = new StringReader("<Root>Content</Root>");
XDocument doc = XDocument.Load(tr);
Console.WriteLine(doc);
This was taken from the MSDN docs for XDocument.Load, found here...
这是从 XDocument.Load 的 MSDN 文档中获取的,在这里找到...