c# - 从 xml 文件中检索元数据

标签 c# .net xml metadata

我需要从多个 xml 文件中检索元数据。 xml文件的结构如下:

<songs>
 <song_title> some title </song_title>
 <classification> some classification </classification>
 <song_text> some text </song_text>
 <singer>
    <sing> singer's name </sing>
    <gender> gender </gender>
    <bornYear> year </bornYear>
    <livePlace> live place </livePlace>
    <liveArea> live area </liveArea>
  </singer>
</songs>

用户选择搜索条件 - 居住地点或居住区域。然后他输入他搜索的地点或区域的名称。我需要找到并显示歌曲的链接,这些歌曲的元数据中包含用户输入的地点或区域。我正在使用 .NET 3.5

最佳答案

这个答案更像是一个指针......

您可以使用 LINQ to XML来完成这个任务。

什么是 LINQ to XML?

LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the .NET Framework programming languages.

LINQ to XML is like the Document Object Model (DOM) in that it brings the XML document into memory. You can query and modify the document, and after you modify it you can save it to a file or serialize it and send it over the Internet. However, LINQ to XML differs from DOM: It provides a new object model that is lighter weight and easier to work with, and that takes advantage of language improvements in Visual C# 2008.

然后您可以使用 LINQ 查询表达式搜索和操作任何 XML 文档元素,如下例所示:

IEnumerable<XElement> partNos =
from item in purchaseOrder.Descendants("Item")
where (int) item.Element("Quantity") *
    (decimal) item.Element("USPrice") > 100
orderby (string)item.Element("PartNumber")
select item;

关于c# - 从 xml 文件中检索元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6179227/

相关文章:

c# - LINQ - 从具有特定标记的 XML 元素中选择 *

c# - 扩展头删除 "_"字符

c# - 将列表中的两个不同实例序列化为单个 json 字符串

c# - 上传后不能直接从服务器删除文件

c# - .NET 5.0 或 .NET Framework 3.5 用于在 Windows 7+ 上部署?

ruby-on-rails - 获取 XML 文件 POST 请求以使用 Ruby on Rails 解析

c# - .NET Web 服务水合物自定义类

c# - 将非 MVC 相关属性应用于 MVC 操作

.net - 无法使用 HTTPS 使 ServiceStack 在 IIS6 中工作

c# - 循环遍历 XDocument 的元素并获取特定属性