The task is to convert the CSV file into XML.
var x = from line in File.ReadAllLines(@"d:\sample.txt")
where !line.StartsWith("#") && line.Length>0
let parts=line.Split(',')
select new
{
XmlFile= new XElement("root",
new XElement("ISBN",parts[0]),
new XElement("Title",parts[1])
)
};
Questions
1 ) How to dispose the stream (Using Statement with LINQ).
2) How to save the selection into “sample.xml” file?
3) How to bind an Xml file to GridView ?(Do i need to use XmlDataSource ?).
4) Using Linq is it possible to create XSD for my XML ? (without using XSD.exe).
1) File.ReadAllLines performs a File.Close, so you shouldn’t have a stream problem.
2) You need to put your Linq query inside your XElement declaration, that way you get an XElement out of it, and not an IEnumerable of XElements (see below)
3 & 4 someone else can answer… 😉
(Warning, following code untested):