xDocument响应返回:
<Result>
<Value>FAIL</Value>
<Message>Error in Cloud/Upload - No Client Account exists</Message>
</Result>
在我的下面代码中.Equals(Fail)我想抛出一个包含的错误 XDocument响应= XDocument.Parse(infoAsString);
if (response.Root.Name.LocalName.Equals("Result"))
{
try
{
if (response.Root.Elements.Equals("FAIL"))
{
throw new Exception("Error:" + //Message from xml);
}
}
答案 0 :(得分:0)
如果您的回复是XmlDocument,请尝试此操作:
response.SelectSingleNode("//Message").InnerText
如果不是更容易使用XmlDocument
,请参阅下文:
XmlDocument respDoc = new XmlDocument();
respDoc.LoadXml(xml_string);
if(respDoc.SelectSingleNode("//Value").InnerText.Trim() == "FAIL")
{
throw new Exception("Error:" + response.SelectSingleNode("//Message").InnerText);
}
如果您想要使用XDocument,请使用:
response.Root.Element("Message").Value
答案 1 :(得分:0)
以下是使用现有XDocument
进行此操作的方法(请注意我对您的支票“FAIL”的评论):
if (response.Root.Name.LocalName.Equals("Result"))
{
try
{
//response.Root.Elements.Equals("FAIL") won't do what you want, try this:
var val = response.Root.Element("Value").Value;
if (val == "FAIL")
{
throw new Exception("Error: " + response.Root.Element("Message").Value);
}
}
}
答案 2 :(得分:0)
如果响应(一旦确定为try / catch
)保证具有您提供的XML结构,则无需<Result>
。
if (response.Root.Name.LocalName == "Result")
{
if (response.Root.Element("Value").Value == "FAIL")
{
throw new Exception("Error: " + response.Root.Element("Message").Value);
}
}