我有一个XML文档如下:
<document id="5509769">
<s id="1">
<time id="T1S" value="00:00:07,910" />
<w alternative="Ciske" id="1.1">CISKE</w>
<w id="1.2">Die</w>
<w alternative="rot" id="1.3">ROT</w>
<time id="T1E" value="00:00:10,910" />
</s>
<s id="2">
<time id="T2S" value="00:00:12,320" />
<w id="2.1">Almal</w>
<w id="2.2">kan</w>
<w id="2.3">maar</w>
<w id="2.4">die</w>
<w id="2.5">Cholera</w>
<w id="2.6">kry</w>
<w id="2.7">,</w>
<time id="T2E" value="00:00:15,690" />
<time id="T3S" value="00:00:15,750" />
<w id="2.8">en</w>
<w id="2.9">vir</w>
<w id="2.10">my</w>
<w id="2.11">part</w>
<w id="2.12">kan</w>
<w id="2.13">almal</w>
<w id="2.14">doodgaan</w>
<w id="2.15">.</w>
<time id="T3E" value="00:00:18,890" />
</s>
<s id="3">
<time id="T4S" value="00:00:18,960" />
<w id="3.1">Ek</w>
<w id="3.2">het</w>
<w id="3.3">nie</w>
<w id="3.4">lus</w>
<w id="3.5">om</w>
<w id="3.6">te</w>
<w id="3.7">leer</w>
<w id="3.8">nie</w>
<w id="3.9">.</w>
<time id="T4E" value="00:00:22,490" />
</s>
</document>
我想使用Linq将其转换为.srt纯文本文件。
例如,此XML节点的输出为:
1
00:00:07,910 --> 00:00:10,910
CISKE Die ROT
2
00:00:12,320 --> 00:00:18,890
Almal kan maar die Cholera kry, en vir my part kan almal doodgaan.
3
00:00:18,960 --> 00:00:22,490
Ek het nie lus om te leer nie.
我试过这个:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(XML);
using (var nodeReader = new XmlNodeReader(xmldoc))
{
nodeReader.MoveToContent();
XDocument xdoc = XDocument.Load(nodeReader);
List<dynamic> elements =
(
from item in xdoc.Descendants("s")
select new
{
Begin = (item.FirstNode as XElement).LastAttribute.Value,
Last = (item.LastNode as XElement).LastAttribute.Value
}
).ToList<dynamic>();
}
但是我无法检索所有w节点值。
答案 0 :(得分:1)
您可以通过从父项获取所有 w 后代来获取所有 w 元素值。
如下面的代码所示, NodeValuesInArray 作为值数组返回,您可以稍后迭代。
另外,我已经包含了空格分隔的值( NodeValuesCommaSeparated ),如果更合适,可以直接用于显示。
PHP_INI_SYSTEM