如何:从 CSV 文件生成 XML

 我来答
庹亿生2977
2013-11-04 · 超过71用户采纳过TA的回答
知道答主
回答量:115
采纳率:50%
帮助的人:128万
展开全部
在C# 版本中,该查询使用 let 子句将每个字符串分隔成字段数组。 C# // Create the text file.string csvString = @"GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager,(503) 555-7555,2732 Baker Blvd.,Eugene,OR,97403,USA HUNGC,Hungry Coyote Import Store,Yoshi Latimer,Sales Representative,(503) 555-6874,City Center Plaza 516 Main St.,Elgin,OR,97827,USA LAZYK,Lazy K Kountry Store,John Steel,Marketing Manager,(509) 555-7969,12 Orchestra Terrace,Walla Walla,WA,99362,USA LETSS,Let's Stop N Shop,Jaime Yorres,Owner,(415) 555-5938,87 Polk St. Suite 5,San Francisco,CA,94117,USA"; File.WriteAllText("cust.csv", csvString); // Read into an array of strings.string[] source = File.ReadAllLines("cust.csv"); XElement cust = new XElement("Root", from str in source let fields = str.Split(',') select new XElement("Customer", new XAttribute("CustomerID", fields[0]), new XElement("CompanyName", fields[1]), new XElement("ContactName", fields[2]), new XElement("ContactTitle", fields[3]), new XElement("Phone", fields[4]), new XElement("FullAddress", new XElement("Address", fields[5]), new XElement("City", fields[6]), new XElement("Region", fields[7]), new XElement("PostalCode", fields[8]), new XElement("Country", fields[9]) ) ) ); Console.WriteLine(cust); Visual Basic ' Create the text file. Dim csvString AsString = "GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager,(503) 555-7555,2732 Baker Blvd.,Eugene,OR,97403,USA" & vbCrLf & _ "HUNGC,Hungry Coyote Import Store,Yoshi Latimer,Sales Representative,(503) 555-6874,City Center Plaza 516 Main St.,Elgin,OR,97827,USA" & vbCrLf & _ "LAZYK,Lazy K Kountry Store,John Steel,Marketing Manager,(509) 555-7969,12 Orchestra Terrace,Walla Walla,WA,99362,USA" & vbCrLf & _ "LETSS,Let's Stop N Shop,Jaime Yorres,Owner,(415) 555-5938,87 Polk St. Suite 5,San Francisco,CA,94117,USA" File.WriteAllText("cust.csv", csvString) ' Read into an array of strings. Dim source AsString() = File.ReadAllLines("cust.csv") Dim cust As XElement = _ Console.WriteLine(cust)这段代码产生以下输出: xmlLang Great Lakes Food MarketHoward SnyderMarketing Manager(503) 555-75552732 Baker Blvd.EugeneOR97403USAHungry Coyote Import StoreYoshi LatimerSales Representative(503) 555-6874City Center Plaza 516 Main St.ElginOR97827USALazy K Kountry StoreJohn SteelMarketing Manager(509) 555-796912 Orchestra TerraceWalla WallaWA99362USALet's Stop N ShopJaime YorresOwner(415) 555-593887 Polk St. Suite 5San FranciscoCA94117USA
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式