articleABS()
方法輸出的以下XML。如何使用條件解析XML?
Conflict <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="#####" type="text/xsl"?> <?xml-stylesheet href="####" type="text/css"?> <status><code>409</code><message>content.article.required.title null</message></status>
我已經創建createURL()
方法,它應讀從articleABS()
輸出並提取從<code>
節點的值。我想使用代碼中的值來添加一些條件。
public string createURL()
{
string uri = "";
var document = XDocument.Parse(articleABS());
var code = document.Descendants("status").Single().Attribute("code").Value;
if (code == "209")
{
articleCDO();
}
else
{
var href = document.Descendants("link").Single().Attribute("title").Value;
href = href.Replace("Article", "");
string id = Regex.Match(href, @"\d+").Value;
var result = (@"https://#####/article/" + id + "/sections");
return uri = result.ToString();
}
return null;
}
然而,當我運行createURL()
功能,我不斷收到以下 錯誤消息。
錯誤消息:Data at the root level is invalid. Line 1, position 1.
代碼行:var document = XDocument.Parse(articleABS());
請指教,我可能會錯誤。謝謝
的 「XML」 文件,該文件與令牌開始'conflict'不是XML。 – spender
在xml數據之前的衝突字怎麼樣?你的數據真的存在嗎? –
嗨,是的XML在開始時包含'衝突'。有沒有可能繞過這一點。當狀態碼等於209時,來自'articleabs'的響應僅輸出'衝突',否則XML以正確的語法開始。感謝您提供任何進一步的建議。 – user3070072