从资源中读取XML

时间:2014-06-02 22:28:38

标签: xml wpf

我正在尝试从资源中读取XML,但它返回错误。这是我的代码;

string s = Properties.Resources.myXMLFile;
XDocument x = XDocument.Load(s);

错误信息如下;

An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code

Additional information: Illegal characters in path.

单步执行代码,我可以在字符串s中看到XML文件。在VS和其他XML解析器中打开文件,该文件不会出现任何错误。为什么会出现此错误?

1 个答案:

答案 0 :(得分:2)

XDocument.Load()期望包含XML文件路径的字符串作为参数。如果您将XML内容作为字符串,则应使用XDocument.Parse()代替:

string s = Properties.Resources.myXMLFile;
XDocument x = XDocument.Parse(s);