资 源 简 介
C# 在LINQ to XML中将XML文件转换为CSV文件的例子源码,转换的结果请参考如图所示:
private void button1_Click(object sender, EventArgs e)
{//在LINQ to XML中将XML文件转换为CSV文件
TextReader MyReader = new StringReader(this.textBox1.Text);
XElement MyCustomers= XElement.Load(MyReader);
MyReader.Close();
string MyInfo =
(from MyElement in MyCustomers.Elements("客户")
select
String.Format("{0},{1},{2},{3},{4}",
(string)MyElement.Element("客户ID"),
(string)MyElement.Element("公司名称"),
(string)MyElement.Element("城市") + (string)MyElement.Element("地址"),
(string)MyElement.Element("联系人姓名"),
Environment.NewLine
)
).Take(10).Aggregate(new StringBuilder(),(MySubString, MyString)=>MySubString.Append(MyString),MySubString=>MySubString.ToString());
MessageBox.Show(MyInfo, "信息提示");
}