Attention: We are retiring the ASP.NET Community Blogs. Learn more >

DataSets magic and VS.NET Project Files

I needed to parse a .csproj file (the C# Visual Studio .NET project file) and add a few references to some assemblies in our nant/nunit test suite.

The obvious way of doing it is to use a DOM and add a node, etc.

The cool and the lazy programmer's way of doing it is using a DataSet:

DataSet ds = new DataSet();

ds.ReadXml("project.csproj");

ds.Tables["Reference"].Rows.Add(new object[] {"System.XML", "System.Xml", ds.Tables["References"].Rows[0][0]});

ds.WriteXml("project.csproj");

You can also write an XSD for the DataSet (ds.WriteXmlSchema("csproj.xsd")), add the XSD to your VS.NET solution, and generate a typed DataSet for it.

By the way, I tried the same approach to save an RSS file, and the problem is that I found no way to add attributes to the DataSet's root node, so I get something like:

<rss>

</rss>

instead of:

<rss version="2.0">

</rss>

 

No Comments