我有一个 XML 文件:TableList.xml
<TableList>
<Table>
<TableName>TableA</TableName>
<Columns>ColumnA1,ColumnA2,ColumnA3,ColumnA4</Columns>
</Table>
<Table>
<TableName>TableB</TableName>
<Columns>ColumnB1,ColumnB2,ColumnB3,ColumnB4</Columns>
</Table>
</TableList>
我想使用 Linq-to-XML 查询此文件,并为我想派生以下格式的每个值
The table - TableA - has columns - ColumnA1,ColumnA2,ColumnA3,ColumnA4.
The table - TableB - has columns - ColumnB1,ColumnB2,ColumnB3,ColumnB4.
并将其创建为单个文本文件 - TableDoc.txt
- 最后。
如何使用 Linq-to-XML 简洁地圆顶?
最佳答案
How can this be dome with brevity using Linq-to-XML?
为了简洁起见,我将把它当作一个挑战......
File.WriteAllLines("TableDoc.txt", XDocument.Load("TableList.xml")
.Descendants("Table")
.Select(t => string.Format("The table - {0} - has columns {1}.",
t.Element("TableName").Value,
t.Element("Columns").Value)));
(为了真正的简洁性,显然删除了所有不必要的空格。)
当然,这仍然给您留下了弄清楚它是如何工作的/为什么工作的挑战 - 并有可能以更具可读性的方式重构它。
关于c# - 查询 XML 文件并创建文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13016054/