我正在尝试从我收到的XML文档中获取特定标记的内容,以响应使用RestSharp发出的发布请求,如下所示:
//Post the xml and receive an xml response
public static string xPostXml(this string destinationUrl, string xml)
{
var client = new RestClient(destinationUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/xml");
request.AddParameter("undefined", xml, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
return response.Content;
}
else
{
Console.Write(string.Format("{0}({1})", response.StatusDescription, response.StatusCode));
}
return null;
}
//parse the xml into tags to evaluate the content of one
public string AResponseIndicatingTheCalcIsWorkingCorrectly()
{
var result = this.endpoint.xPostXml(this.requestXml);
var doc = XDocument.Parse(result);
var answer = doc.Descendants().Where(x => x.Name.LocalName == "Income");
var myElement = doc.Descendants("Income").FirstOrDefault();
var income = XElementExtension.InnerXML(myElement);
return income;
}
但是,这不起作用,因为xml返回如下:
<Result>k__BackingField":{"<Income>k__BackingField":14800.8,"<Investment>k__BackingField":499850.0,"<PricingVersion>k__BackingField":{"<EffectiveDate>k__BackingField":null,"
等等
使用另一种方法,这样返回Xml(不是从头开始,但可以看到不同的标记格式),它已被上述方法成功解析:
<Income>14800.8</Income><Investment>499850</Investment><PricingVersion><EffectiveDate xsi:nil="true" /><Version>v1137</Version></PricingVersion><MortalityMethod>HRe pos</MortalityMethod>
所以我要么能够生成第二个xml输出示例,要么解析第一个示例并每次查看特定标记。
感谢:)
编辑:
我的朋友在下面通知我这是一个Json字符串,所以我试图解析它如下:
public string doStuff(string xml)
{
dynamic data = JObject.Parse(xml);
return data.Income;
}
虽然返回null。 我在方法AResponseIndicatingTheCalcIsWorking中调用此方法并返回doStuff(xml)。