protected internal void LoadXmlFragment(IPersistXMLFragment persistXmlFragment, string configName)
{
if (persistXmlFragment == null)
{
throw new ArgumentNullException("persistXmlFragment");
}
if (xmlFragments == null)
{
// Retrieve the xml fragments from MSBuild
xmlFragments = new XmlDocument();
var fragments = GetProjectExtensions()[ProjectFileConstants.VisualStudio];
fragments = string.Format(CultureInfo.InvariantCulture, "<root>{0}</root>", fragments);
xmlFragments.LoadXml(fragments);
}
// We need to loop through all the flavors
string flavorsGuid;
ErrorHandler.ThrowOnFailure(((IVsAggregatableProject) this).GetAggregateProjectTypeGuids(out flavorsGuid));
foreach (var flavor in Utilities.GuidsArrayFromSemicolonDelimitedStringOfGuids(flavorsGuid))
{
// Look for a matching fragment
var flavorGuidString = flavor.ToString("B");
string fragment = null;
XmlNode node = null;
foreach (XmlNode child in xmlFragments.FirstChild.ChildNodes)
{
if (child.Attributes.Count > 0)
{
var guid = string.Empty;
var configuration = string.Empty;
if (child.Attributes[ProjectFileConstants.Guid] != null)
guid = child.Attributes[ProjectFileConstants.Guid].Value;
if (child.Attributes[ProjectFileConstants.Configuration] != null)
configuration = child.Attributes[ProjectFileConstants.Configuration].Value;
if (
string.Compare(child.Name, ProjectFileConstants.FlavorProperties,
StringComparison.OrdinalIgnoreCase) == 0
&& string.Compare(guid, flavorGuidString, StringComparison.OrdinalIgnoreCase) == 0
&& ((string.IsNullOrEmpty(configName) && string.IsNullOrEmpty(configuration))
|| (string.Compare(configuration, configName, StringComparison.OrdinalIgnoreCase) == 0)))
{
// we found the matching fragment
fragment = child.InnerXml;
node = child;
break;
}
}
}
var flavorGuid = flavor;
if (string.IsNullOrEmpty(fragment))
{
// the fragment was not found so init with default values
ErrorHandler.ThrowOnFailure(persistXmlFragment.InitNew(ref flavorGuid,
(uint) _PersistStorageType.PST_PROJECT_FILE));
// While we don't yet support user files, our flavors might, so we will store that in the project file until then
// TODO: Refactor this code when we support user files
ErrorHandler.ThrowOnFailure(persistXmlFragment.InitNew(ref flavorGuid,
(uint) _PersistStorageType.PST_USER_FILE));
}
else
{
ErrorHandler.ThrowOnFailure(persistXmlFragment.Load(ref flavorGuid,
(uint) _PersistStorageType.PST_PROJECT_FILE, fragment));
// While we don't yet support user files, our flavors might, so we will store that in the project file until then
// TODO: Refactor this code when we support user files
if (node.NextSibling != null && node.NextSibling.Attributes[ProjectFileConstants.User] != null)
ErrorHandler.ThrowOnFailure(persistXmlFragment.Load(ref flavorGuid,
(uint) _PersistStorageType.PST_USER_FILE, node.NextSibling.InnerXml));
}
}
}