【发布时间】:2015-12-07 05:40:16
【问题描述】:
我有 xml。我正在使用此代码检索信息:
XNamespace ns = "urn:schemas-microsoft-com:office:excel";
var xdoc = XDocument.Load(@"path");
IEnumerable<string> ss = xdoc.Descendants(ns + "Crn")
.Elements(ns + "Text")
.Select(x => (string)x);
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"path", true))
{
foreach (var x in ss)
{
file.WriteLine(x);
}
}
但我只想检索 last - 1 标记文本。我在那里添加了文本“this text”。我怎样才能做到这一点?
如果我在 .Element 之后添加.LastOrDefault() 会引发错误..
这是我的 XML
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<SupBook>
<Xct>
<Crn>
<Row>9</Row>
<ColFirst>1</ColFirst>
<ColLast>3</ColLast>
<Text>02</Text>
<Text>this text</Text>
<Text>asd</Text>
</Crn>
<Crn>
<Row>10</Row>
<ColFirst>1</ColFirst>
<ColLast>3</ColLast>
<Number>25</Number>
<Text>this text</Text>
<Text>asd</Text>
</Crn>
<Crn>
<Row>11</Row>
<ColFirst>1</ColFirst>
<ColLast>3</ColLast>
<Number>62</Number>
<Text>this text</Text>
<Text>asd</Text>
</Crn>
</Xct>
</SupBook>
</ExcelWorkbook>
</Workbook>
我无法更改该 xml 文件。我知道必须有列名 而不是文本标签,但不是我写的
【问题讨论】:
标签: c# xml linq linq-to-xml