简体   繁体   English

MVC从Web服务解析XML

[英]MVC Parse XML from Web Service

I am trying to parse the xml results from a web service but I am getting an error {The best overloaded method match for 'System.Xml.Linq.XElement.Parse(string)' has some invalid arguments"} 我正在尝试从Web服务解析xml结果,但出现错误{The best overloaded method match for 'System.Xml.Linq.XElement.Parse(string)' has some invalid arguments"}

var service = new RemoteService.RemoteSoapClient("RemoteSoap");
var result = service.GetDetails(systemId, appId);

ViewBag.Surname = XElement.Parse(result).Descendants("Customer").Single().Attribute("Surname").Value;

XML Output XML输出

<?xml version="1.0" encoding="UTF-8"?>
-<List>
-<Customer>
<Surname>Spack</Surname>
<Firstname>Bob</Firstname>
</Customer>
</List>

What am I doing wrong, please help. 我做错了什么,请帮忙。

If you F12 XElement.Parse you can see it only has two separate method signatures, in both of which the expected first parameter is a string. 如果您使用F12 XElement.Parse,则可以看到它只有两个单独的方法签名,这两个签名中期望的第一个参数是字符串。 I'm going to guess service.GetDetails does not return a string. 我要猜测service.GetDetails不返回字符串。

If you throw a .ToString() at the end of your ViewBag.Surname assignment that should at least get you out of your compile error. 如果在ViewBag.Surname分配的末尾抛出.ToString(),则至少应使您摆脱编译错误。 Whether or not that actually gets you the data you expect, hard to say. 很难说,这是否真的为您提供了您期望的数据。

As the error message suggested, XElement.Parse() expect string as argument (instead of XmlDocument as you currently passed). 根据错误消息的建议, XElement.Parse()期望将string作为参数(而不是当前传递的XmlDocument )。

You can use XmlDocument.OuterXml to get string markup representation of XmlDocument : 您可以使用XmlDocument.OuterXml得到的字符串标记表示XmlDocument

ViewBag.Surname = XElement.Parse(result.OuterXml)
                          .Descendants("Customer")
                          .Single()
                          .Element("Surname")
                          .Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM