【问题标题】:Want to extract tags from XElements and display as grid on ASP.NET Webform想要从 XElements 中提取标签并在 ASP.NET Webform 上显示为网格
【发布时间】:2016-04-24 13:26:52
【问题描述】:

我正在编写云服务并将 ASP.NET Web 角色与 WebForm 一起使用。

在我的代码中,我在 XElement 中获取数据,现在我想从中提取数据并在 WebForm 上以表格或网格格式显示

我的XElement 包含几个<entry> 标签,如下所示:

<entry xml:base="https://STORAGE_ACCOUNT.table.core.windows.net/"           xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"     xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"         m:etag="W/"datetime'2013-09-08T07%3A19%3A07.2189243Z'""    xmlns="http://www.w3.org/2005/Atom">
  <id>https://STORAGE_ACCOUNT.table.core.windows.net/authors(PartitionKey='Beckett',RowKey='Molloy')</id>
  <title type="text"></title>
  <updated>2013-09-08T07:19:07Z</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="authors" href="authors(PartitionKey='Beckett',RowKey='Molloy')" />
  <category term="STORAGE_ACCOUNT.authors" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:PartitionKey>Beckett</d:PartitionKey>
      <d:RowKey>Molloy</d:RowKey>
      <d:Timestamp m:type="Edm.DateTime">2013-09-08T07:19:07.2189243Z</d:Timestamp>
      <d:Artist>Beckett</d:Artist>
      <d:Title>Molloy</d:Title>
    </m:properties>
  </content>
</entry>

我想提取以下标签

<d:Artist>Beckett</d:Artist>
<d:Title>Molloy</d:Title>

并在 ASPX 网络表单上以表格格式显示数据,如下所示

Artist   Title
Beckett  Moelly

如何在我的代码中执行此操作?

我看到了一些绑定到数据集的示例,但它适用于某些驱动器上的 xml 文件,但因为我的代码中有它。我还看到有人建议使用 XSLT 将 XML 转换为 HTML,然后显示它,但我不知道如何在代码中执行此操作。请给我指点

【问题讨论】:

    标签: c# asp.net xml xslt webforms


    【解决方案1】:

    好吧,假设您确实想使用 LINQ to XML 而不是其他选项之一来执行此操作,这非常简单:

    XNamespace atom = "http://www.w3.org/2005/Atom"
    XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"
    
    foreach (var entry in element.Descendants(atom + "entry"))
    {
        var artist = (string)entry.Descendants(d + "Artist").Single();
        var title = (string)entry.Descendants(d + "Title").Single();
    
        // ... do something with these values
    }
    

    【讨论】:

    • 谢谢。我试过你说的。但是,我的 xelement 包含 标签序列,如上所示。
    • @Fairy 我更新了以展示您将如何遍历每个条目并获取艺术家和标题值。
    • 谢谢。我以同样的方式对序列进行了处理。现在我想弄清楚如何在 MVC 视图上将艺术家和标题数据显示为表格。我是 ASP.NEt 和 MVC 的新手,并且正在弄清楚如何将其传递给模型。我的模型看起来像 public class DataModel { public string Artist { get;放; } 公共字符串作者 { 获取;放; } } 和我的控制器是这样的 public ActionResult Index() { Xelement entity = TableHelper.GetEntities("table1");返回视图(); }。视图是使用 DataModel 使用列表模板创建的
    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多