使用Linq从XDocument获取数据



我正在尝试从API获得的XML响应中获取URL。到目前为止,我一直在尝试使用这个代码,但我被卡住了:

using (StringReader s = new StringReader(result)) //result is the xml returned by the api
{
XDocument xDoc;
xDoc = XDocument.Load(s);
var filterXml = xDoc.Descendants("content");
Console.WriteLine(filterXml);
}

这是我试图阅读的XML示例:

<feed xml:base="http://someurl/Seg/_vti_bin/Listdata.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Documents00</title>
<id>http://someurl.com.do/Seg/_vti_bin/Listdata.svc/Documents00</id>
<updated>2020-11-22T06:09:33Z</updated>
<link rel="self" title="Documentos008046" href="Documentos008046" />
<entry m:etag="W/&quot;3&quot;">
<id>http://someurl/Seg/_vti_bin/Listdata.svc/Documents00(805)</id>
<title type="text"></title>
<updated>2020-11-19T15:02:20-04:00</updated>
<author>
<name />
</author>
<link m:etag="&quot;{3149A72A-9FBA-40B0-A8EA-B0FB7E46C549},4&quot;" rel="edit-media" title="Documents00Item" href="Documents00(805)/$value" />
<link rel="edit" title="Documentos008046Item" href="Documentos008046(805)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreadoPor" type="application/atom+xml;type=entry" title="CreadoPor" href="Documentos008046(805)/CreadoPor" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModificadoPor" type="application/atom+xml;type=entry" title="ModificadoPor" href="Documents00(805)/ModificadoPor" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/DesprotegidoPara" type="application/atom+xml;type=entry" title="DesprotegidoPara" href="Documents00(805)/DesprotegidoPara" />
<category term="Microsoft.SharePoint.DataService.Documentos008046Item" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/octetstream" src="http://someurl/Seg/Documents00/somefiletodownload.xlsx" />
<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<d:IdDeTiposDeContenido>0x01010042419E2F2397694F91DDB2DF1A437E9A</d:IdDeTiposDeContenido>
<d:Nombre>somefiletodownload.xlsx</d:Nombre>
<d:Título m:null="true"></d:Título>
<d:Cliente>Some Costumer</d:Cliente>
<d:TipoCliente>I</d:TipoCliente>
<d:TipoArchivo>N</d:TipoArchivo>
<d:Producto>SEGURO DE VIDA COLECTIVO</d:Producto>
<d:Anio>2020</d:Anio>
<d:Mes>09</d:Mes>
<d:Dia>16</d:Dia>
<d:Enlace>VuPnHC81l6n4GE4xXndbFg00</d:Enlace>
<d:Oficina>OFICINA PRINCIPAL</d:Oficina>
<d:Ramo>AUTO</d:Ramo>
<d:Poliza>SVC-2481</d:Poliza>
<d:IndicadorEmail>Si</d:IndicadorEmail>
<d:TipoDocumento>POLIZA</d:TipoDocumento>
<d:Documento>somefiletodownload.xlsx</d:Documento>
<d:Identificador m:type="Edm.Int32">805</d:Identificador>
<d:TipoDeContenido>Documento</d:TipoDeContenido>
<d:Creado m:type="Edm.DateTime">2020-11-19T14:58:55</d:Creado>
<d:CreadoPorId m:type="Edm.Int32">30084</d:CreadoPorId>
<d:Modificado m:type="Edm.DateTime">2020-11-19T15:02:20</d:Modificado>
<d:ModificadoPorId m:type="Edm.Int32">30084</d:ModificadoPorId>
<d:CopiarOrigen m:null="true"></d:CopiarOrigen>
<d:EstadoDeAprobación>0</d:EstadoDeAprobación>
<d:RutaDeAcceso>/seg/Documents00</d:RutaDeAcceso>
<d:DesprotegidoParaId m:type="Edm.Int32" m:null="true"></d:DesprotegidoParaId>
<d:EstadoDelVirus>16607</d:EstadoDelVirus>
<d:EsLaVersiónActual m:type="Edm.Boolean">true</d:EsLaVersiónActual>
<d:Owshiddenversion m:type="Edm.Int32">3</d:Owshiddenversion>
<d:Versión>1.0</d:Versión>
</m:properties>
</entry>
</feed>

我需要属性";src";内部";内容";它包含文件的url,所以我可以下载它。

在搜索Descendant时需要包含命名空间。例如,

using (StringReader s = new StringReader(result)) 
{
var xDoc = XDocument.Load(s);
XNamespace ns = "http://www.w3.org/2005/Atom";
var filterXml = xDoc.Descendants(ns+"content").First();
var url = filterXml.Attribute("src").Value;
}

Descendants需要使用名称空间和Descendant Name的组合进行筛选。

过滤子体后,可以使用.Attribute("src").Value获得所需属性的值

演示

最新更新