我正在使用linq解析xml,但它没有返回结果:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<downloadInfoResponse xmlns="http://webService">
<downloadInfoReturn>
<city>city</city>
<companyName>company name</companyName>
</downloadInfoReturn>
</downloadInfoResponse>
</soapenv:Body>
</soapenv:Envelope>
代码:
public class Merc
{
public string CompanyName { get; set; }
}
using (XmlReader reader = XmlReader.Create(new StringReader(result)))
{
XDocument doc = XDocument.Load(reader, LoadOptions.SetLineInfo);
List<Merc> m = (from downloadInfoReturn in doc.Descendants("downloadInfoReturn")
select new Merc
{
CompanyName = downloadMerchantInfoReturn.Element("companyName").Value
}).ToList();
}
还有别的好办法吗?谢谢您。
最佳答案
XML文件包含命名空间,因此在执行查询时需要指定它:
XNamespace xn = "http://webService";
doc.Descendants(xn + "downloadInfoReturn")