将 Xml 转换为 txt
我目前正在使用一个 XML 文件,该文件以 XML 格式保存比赛信息,所以
<Row xmlns="Practice2a">
<RecordType>Qualifying Classification</RecordType>
<_x0030_02150Position>3</_x0030_02150Position>
<Class>250</Class>
<_x0030_02150MachineNo>11</_x0030_02150MachineNo>
<RiderName>Kevin James</RiderName>
<Machine>Honda</Machine>
<_x0030_02150ToDBehind>29.680</_x0030_02150ToDBehind>
<_x0030_02150BestLapSpeed>97.1415157615475</_x0030_02150BestLapSpeed>
<_x0030_02150ToDBestLapTime>5:32.274</_x0030_02150ToDBestLapTime>
<_x0030_02150BestOnLap>7</_x0030_02150BestOnLap>
</Row>
我想创建一个仅包含部分信息的纯 txt 文件,我只想以表格格式保存,例如
pos Name racetime and BestLaptime
我尝试删除标签从文件中创建一个 txt 文件,所以现在我得到 我创建了一个行计数,可能用作提取正确字段的分隔符。
139 Qualifying Classification
140 3
141 250
142 11
Driver Name: Machine Type: Kevin James
145 Honda
146 29.680
147 97.1415157615475
148 5:32.274
我的代码已经完全失控了,我想知道是否有更好的方法来实现这一点,而不是每次都添加 14 来计数,这就是我显示驱动程序名称:“而不是数字的方式。
有关如何进行的任何指示你会去做这件事,这将是一个很好的见解。
I am currently working with an XML file that keeps race information in XML format like so
<Row xmlns="Practice2a">
<RecordType>Qualifying Classification</RecordType>
<_x0030_02150Position>3</_x0030_02150Position>
<Class>250</Class>
<_x0030_02150MachineNo>11</_x0030_02150MachineNo>
<RiderName>Kevin James</RiderName>
<Machine>Honda</Machine>
<_x0030_02150ToDBehind>29.680</_x0030_02150ToDBehind>
<_x0030_02150BestLapSpeed>97.1415157615475</_x0030_02150BestLapSpeed>
<_x0030_02150ToDBestLapTime>5:32.274</_x0030_02150ToDBestLapTime>
<_x0030_02150BestOnLap>7</_x0030_02150BestOnLap>
</Row>
I want to create a plain txt file with just some of the information , I just want in kind off in a table format e.g
pos Name racetime and BestLaptime
I have attempted to remove the tags from the file and create a txt file so now I get
I create a line count to possibly use as delimiters for extracting the right fields.
139 Qualifying Classification
140 3
141 250
142 11
Driver Name: Machine Type: Kevin James
145 Honda
146 29.680
147 97.1415157615475
148 5:32.274
My code is getting quite out of hand and I am wondering if there is a much better way to achieve this rather than adding 14 to count each time , that's how i am displaying Driver Name:" instead of a number.
Any pointers as to how you would go about this would be a great insight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个快速的解决方案是读取 XmlDocument 中的 xml(或者更简单的数据集),并在 C# 代码中生成文本文件。
请参阅:
另一种方法是定义一个 xslt,将 xml 重新格式化为您选择的布局。通常,它是从 xml 数据生成 html 文档的首选方法,但也可用于转换为普通文本报告。您可以在
A quick solution would be to read your xml in XmlDocument (or even simpler to a dataset), and generate the text file in your c# code.
See:
Alternate approach would be to define an xslt to reformat your xml to layout of your choice. Normally its a preferred approach for generating html docs from your xml datam, though could be used to transform into normal text reports. You can read more about it on
您可以使用 LinqToXml 对其进行解析和格式化:
当然,您需要根据您的需要调整 FormatElement 方法,但此方案应该可行。
You can parse and format it using LinqToXml:
Of course you need to adapt the FormatElement method to your needs, but this scheme should work.
XML 的设计使得某些功能是必需的并且可能是依赖的,而其他功能则是文档作者的选择。您的方案似乎完全倒退了这些功能!标准不保证某些内容出现在哪一行。整个合法的 XML 文件可能占用一行。
XML 的全部要点在于,使用标准格式可以使用通用工具。 .NET Framework 内置了(几个)XML 解析组件,它们可以读取此文件并为您提供所需的准确信息。然后,您可以将该信息输出为您喜欢的任何格式的文本。
没有理由自己解析它。
请记住,如果您的解决方案包含 RegEx,那么您就已经失败了。
(我在开玩笑最后一部分。有点。)
XML is designed so that some features are required and may be depended-on, while other things are the choice of the author of the document. Your scheme seems to get those features exactly backwards! Which line something appears on is not guaranteed by the standard. An entire legal XML file may occupy a single line.
The whole point of XML is that the use of a standard format allows for the use of common tools. The .NET Framework has (several) XML parsing components built in to it that can read this file and give you exactly the information you are looking for. You can then output that information as text in whatever format you like.
There is no reason to parse it yourself.
And remember, if your solution includes RegEx, then you've already lost.
(I'm kidding about that last part. Sort of.)