Ok I have one solution:
string GetWebConfigXml() {
string configXml = null;
Window window = null;
ProjectItem project = null;
ProjectItem configFile = null;
try {
EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE)) as DTE;
if(dte == null) return null;
project = dte.Solution.FindProjectItem(dte.ActiveDocument.FullName);
configFile = (from ProjectItem childItem in project.ProjectItems
where childItem.Name.Equals("web.config", StringComparison.OrdinalIgnoreCase)
select childItem).FirstOrDefault();
if (configFile == null) return null;
if (!configFile.IsOpen) window = configFile.Open();
var selection = (TextSelection)configFile.Document.Selection;
selection.SelectAll();
configXml = selection.Text;
} finally {
//Clean up the COM stuff
if (window != null) {
window.Close(vsSaveChanges.vsSaveChangesNo);
window = null;
}
if (configFile != null) {
configFile = null;
}
if (project != null) {
project = null;
}
}
}
return configXml;
}
Note: Don't forget you'll probably need a boat load of try catches in here