我正在从网站上搜索公司名称。网站以XML格式返回响应。我试图将XML转换为自定义列表。我已将XML响应转换为XDocument,但未检索到我尝试获取的节点值。我之前没有这样做,所以我不知道该怎么做。下面是XML响应,
.Set<T>
因为我是新手,所以我尝试不同的东西。以下所有行都不返回任何数据,
<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd">
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>NameSearch</Class>
<Qualifier>response</Qualifier>
<TransactionID>Transaction ID</TransactionID>
<GatewayTimestamp>Time</GatewayTimestamp>
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>SenderID</SenderID>
<Authentication>
<Method>CHMD5</Method>
<Value />
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys />
</GovTalkDetails>
<Body>
<NameSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NameSearch.xsd">
<ContinuationKey>Some Key</ContinuationKey>
<RegressionKey>Some Key</RegressionKey>
<SearchRows>3</SearchRows>
<CoSearchItem>
<CompanyName>Company 1</CompanyName>
<CompanyNumber>1212323</CompanyNumber>
</CoSearchItem>
<CoSearchItem>
<CompanyName>Company 2</CompanyName>
<CompanyNumber>2122421</CompanyNumber>
</CoSearchItem>
<CoSearchItem>
<CompanyName>Company 3</CompanyName>
<CompanyNumber>0655421</CompanyNumber>
</CoSearchItem>
</NameSearch>
</Body>
</GovTalkMessage>
请指导我如何检索每个CoSearchItem节点的公司名称和公司编号。感谢
答案 0 :(得分:1)
指定命名空间:
XNamespace ns = "http://xmlgw.companieshouse.gov.uk/v1-0/schema";
var results = xDocument.Descendants(ns + "CoSearchItem")
.Select(n => new
{
CompanyName = n.Element(ns +"CompanyName").Value,
CompanyNumber = n.Element(ns +"CompanyNumber").Value
})
.ToList();