【问题标题】:Getting data from XDocument using Linq使用 Linq 从 XDocument 获取数据
【发布时间】:2020-11-22 13:49:29
【问题描述】:

我正在尝试从从 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>

我需要在“content”中包含文件 url 的属性“src”,以便我可以下载它。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    您需要在搜索 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 需要使用命名空间和后代名称的组合进行过滤。

    过滤后代后,您可以使用.Attribute("src").Value获取所需属性的值

    Demo

    【讨论】:

    • @Jaracara11 很高兴它有帮助。请将其标记为答案/有帮助,以便它可以帮助任何有类似问题的人
    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 2022-07-02
    • 1970-01-01
    • 2019-07-23
    • 2012-10-13
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多