How to generate text files from XML (LINQ to XML)
This article provides an example that shows how to use C# and Visual Basic to generate a comma-separated values (CSV) text file from an XML file.
Example: Generate a CSV file from an XML document
This example generates a CSV file from XML document Sample XML file: Customers and orders.
The C# version uses method syntax and the Aggregate
operator to generate the file in a single expression. For more information, see Query Syntax and Method Syntax in LINQ (C#).
The Visual Basic version uses procedural code to aggregate the collection of strings into a single string.
XElement custOrd = XElement.Load("CustomersOrders.xml");
string csv =
(from el in custOrd.Element("Customers").Elements("Customer")
select
String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}{10}",
(string)el.Attribute("CustomerID"),
(string)el.Element("CompanyName"),
(string)el.Element("ContactName"),
(string)el.Element("ContactTitle"),
(string)el.Element("Phone"),
(string)el.Element("FullAddress").Element("Address"),
(string)el.Element("FullAddress").Element("City"),
(string)el.Element("FullAddress").Element("Region"),
(string)el.Element("FullAddress").Element("PostalCode"),
(string)el.Element("FullAddress").Element("Country"),
Environment.NewLine
)
)
.Aggregate(
new StringBuilder(),
(sb, s) => sb.Append(s),
sb => sb.ToString()
);
Console.WriteLine(csv);
Dim custOrd As XElement = XElement.Load("CustomersOrders.xml")
Dim strCollection As IEnumerable(Of String) = _
From el In custOrd.<Customers>.<Customer> _
Select _
String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}{10}", _
el.@CustomerID, _
el.<CompanyName>.Value, _
el.<ContactName>.Value, _
el.<ContactTitle>.Value, _
el.<Phone>.Value, _
el.<FullAddress>.<Address>.Value, _
el.<FullAddress>.<City>.Value, _
el.<FullAddress>.<Region>.Value, _
el.<FullAddress>.<PostalCode>.Value, _
el.<FullAddress>.<Country>.Value, _
Environment.NewLine _
)
Dim sb As StringBuilder = New StringBuilder()
For Each str As String In strCollection
sb.Append(str)
Next
Console.WriteLine(sb.ToString())
This example produces the following output:
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
See also
Phản hồi
https://aka.ms/ContentUserFeedback.
Sắp ra mắt: Trong năm 2024, chúng tôi sẽ dần gỡ bỏ Sự cố với GitHub dưới dạng cơ chế phản hồi cho nội dung và thay thế bằng hệ thống phản hồi mới. Để biết thêm thông tin, hãy xem:Gửi và xem ý kiến phản hồi dành cho