【发布时间】:2014-03-30 09:55:19
【问题描述】:
我有如下所示的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<record id="177" restricted="false">
<type>record type</type>
<startdate>2000-10-10</startdate>
<enddate>2014-02-01</enddate>
<titles>
<title xml:lang="en" type="main">Main title</title>
<!-- only one title element with type main -->
<title xml:lang="de" type="official">German title</title>
<!-- can have more titles of type official -->
</titles>
<description>description of the record</description>
<categories>
<category id="122">
<name>category name</name>
<description>category description</description>
</category>
<!-- can have more categories -->
</categories>
<tags>
<tag id="5434">
<name>tag name</name>
<description>tag description</description>
</tag>
<!-- can have more tags -->
</tags>
</record>
如何使用 LINQ 从这些 xml 文件中选择数据,还是应该使用其他方法?
【问题讨论】:
-
LINQ to XML 是一个不错的选择。你应该试试看。
-
鉴于您知道架构,我建议您先将其反序列化为对象。然后在 C# 中更容易使用。
-
请说明您要选择的数据。 XElement class 是当今在 C# 中使用 LINQ 和 XML 的首选方式。
-
@Cupcake 该文件包含更多数据,但我需要此处指定的数据(记录类型、开始日期、结束日期、标题主要和所有翻译、带有 id、名称和描述的类别以及与标签相同)。你有任何例子吗,我知道如何创建 XDocument 并添加带有属性的 XElements,然后将其保存到文件中,但如何以其他方式进行(xml 字符串到 XElemet 和 XDocument)。
-
@Aron 你的意思是使用 XMLFormatter 和 Stream 反序列化它?
标签: c# .net xml linq xml-parsing