在VB中将ASCII文本文件转换为XML的最佳方法.. NET, c#或Javascript
本文关键字:NET 方法 最佳 Javascript XML ASCII 中将 VB 文本 文件 转换 | 更新日期: 2023-09-27 18:05:50
我不确定这是否是要问的正确问题,但我会告诉你我在尝试什么。我有一个ASCII文本文件与产品数据在它。它没有标头。我正在寻找一种将此数据转换为XML文件的方法,但是,我希望它通过一个txt字段进行解析,以合并查找字段并根据该字段修改数据。现在我可以用VB了。. NET, c#或Javascript。示例:
示例ASCII数据:
ACE14 1016 ACP, Inc. Amana® Commercial Convection Express™ Combination Oven
AOC24 1016 ACP, Inc. Amana® Commercial Microwave Oven, 2400 watts
示例XML我需要基于给出的示例(它将查找基于'ACP, Inc.'的信息)
<xml>
<Product>
<ProductManufacturer>ACP, Inc.</ProductManufacturer>
<ProductCode>AMN-ACE14</ProductCode>
<ProductTitle>Amana Commercial Convection Express Combination Oven</ProductTitle>
</Product>
<Product>
<ProductManufacturer>ACP, Inc.</ProductManufacturer>
<ProductCode>AMN-AOC24</ProductCode>
<ProductTitle>Amana Commercial Microwave Oven</ProductTitle>
</Product>
谁能给我介绍一些好的样品?这会给我一个好的开始。谢谢。
这是一个简单的解决方案,但正如我在评论中暗示的那样,确实有很多方法可以做到这一点。根据您的需求选择一个(是否需要以稍微不同的格式读取不同的文件类型-然后选择库)。
var lines = File.ReadAllLines("D:''test.txt");
var products = from line in lines
select new
{
ProductManufacturer = line.Substring(0,12).Trim(),
ProductCode = line.Substring(12, 8).Trim(),
Description = line.Substring(20).Trim()
};
var xml = new XDocument(new XElement("xml",
from p in products
select new XElement("Product",
new XElement("ProductManufacturer", p.ProductManufacturer),
new XElement("ProductCode", p.ProductCode),
new XElement("Description", p.Description))));
您还可以考虑创建一个Product类,然后使用XML序列化而不是在代码中指定格式。如果您需要经常使用Product实体,这可能是一个好主意。