未从 XDocument 获取所需的元素
Not getting desired elements from XDocument
我正在从网站搜索公司名称。网站 return 的响应为 XML。我正在尝试将 XML 转换为自定义列表。我已将 XML 响应转换为 XDocument,但未检索到我尝试获取的节点值。我以前没有这样做过,所以我不确定该怎么做。下面是 XML 响应,
<?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>
因为我是新手所以我正在尝试不同的东西。以下所有行return无数据,
var v1 = xDocument.Descendants("GovTalkMessage").Elements("CoSearchItem");
var v2 = xDocument.Descendants("Body").Elements("CoSearchItem");
var v3 = xDocument.Descendants("NameSearch").Elements("CoSearchItem");
请指导我如何为每个 CoSearchItem 节点检索公司名称和公司编号。谢谢
命名空间的帐户:
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();
我正在从网站搜索公司名称。网站 return 的响应为 XML。我正在尝试将 XML 转换为自定义列表。我已将 XML 响应转换为 XDocument,但未检索到我尝试获取的节点值。我以前没有这样做过,所以我不确定该怎么做。下面是 XML 响应,
<?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>
因为我是新手所以我正在尝试不同的东西。以下所有行return无数据,
var v1 = xDocument.Descendants("GovTalkMessage").Elements("CoSearchItem");
var v2 = xDocument.Descendants("Body").Elements("CoSearchItem");
var v3 = xDocument.Descendants("NameSearch").Elements("CoSearchItem");
请指导我如何为每个 CoSearchItem 节点检索公司名称和公司编号。谢谢
命名空间的帐户:
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();