路径中的非法字符

I am querying a soap based service and wish to analyze the XML returned however when I try to load the XML into an XDoc in order to query the data. am getting an 'illegal characters in path' error message? This (below) is the XML returned from the service. I simply want to get the list of competitions and put them into a List I have setup. The XML does load into an XML Document though so must be correctly formatted?.

任何关于如何做到这一点并避免错误的最佳方法的建议,我们都将不胜感激。

<?xml version="1.0" ?>
- <gsmrs version="2.0" sport="soccer" lang="en" last_generated="2010-08-27 20:40:05">
- <method method_id="3" name="get_competitions">
<parameter name="area_id" value="1" />
<parameter name="authorized" value="yes" />
<parameter name="lang" value="en" />
</method>
<competition competition_id="11" name="2. Bundesliga" soccertype="default" teamtype="default" display_order="20" type="club" area_id="80" last_updated="2010-08-27 19:53:14" area_name="Germany" countrycode="DEU" />
</gsmrs>

下面是我的代码,我需要能够在 XDoc 中查询数据:

string theXml = myGSM.get_competitions("", "", 1, "en", "yes");
XmlDocument myDoc = new XmlDocument();
MyDoc.LoadXml(theXml);
XDocument xDoc = XDocument.Load(myDoc.InnerXml);
89298 次浏览

你不显示你的源代码,但是我猜你正在做的是:

string xml = ... retrieve ...;
XmlDocument doc = new XmlDocument();
doc.Load(xml); // error thrown here

Load方法需要的是 文件名而不是 XML 本身:

... same code ...
doc.LoadXml(xml);

类似地,使用 XDocumentLoad(string)方法需要的是 文件名,而不是实际的 XML。但是,没有 LoadXml方法,因此从字符串加载 XML 的正确方法如下:

string xml = ... retrieve ...;
XDocument doc;
using (StringReader s = new StringReader(xml))
{
doc = XDocument.Load(s);
}

事实上,在开发任何东西时,不仅要注意参数的类型,还要注意参数的 语义学(含义)。当参数的类型是 string时,并不意味着可以只提供字符串类型的任何内容。

关于你更新的问题,同时使用 XmlDocumentXDocument是没有意义的。选择其中一个。

如果这真的是您的输出,那么它就是非法的 XML,因为它使用了减号(’-’)。我怀疑你已经剪切和粘贴了浏览器,如 IE。必须从文本编辑器而不是浏览器显示确切的 XML。

接下来是 Ondrej Tucny 的回答:

如果希望改为使用 xml 字符串,可以使用 XElement,并调用“ parse”方法。(因为根据您的需要,XElement 和 XDocument 将满足您的需要)

例如:

string theXML = '... get something xml-ish...';
XElement xEle = XElement.Parse(theXML);


// do something with your XElement

XElement 的 Parse 方法允许传入 XML 字符串,而 Load 方法需要一个文件名。

为什么不呢

XDocument.Parse(theXml);

我想这是正确的解决方案