VsTeXProject.VisualStudio.Project.ProjectNode.LoadXmlFragment C# (CSharp) Метод

LoadXmlFragment() защищенный Метод

Initialize an object with an XML fragment.
protected LoadXmlFragment ( IPersistXMLFragment persistXmlFragment, string configName ) : void
persistXmlFragment IPersistXMLFragment
configName string Name of the configuration being initialized, null if it is the project
Результат void
        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));
                }
            }
        }

Usage Example

Пример #1
0
        public ProjectConfig(ProjectNode project, string configuration)
        {
            ProjectMgr = project;
            ConfigName = configuration;

            // Because the project can be aggregated by a flavor, we need to make sure
            // we get the outer most implementation of that interface (hence: project --> IUnknown --> Interface)
            var projectUnknown = Marshal.GetIUnknownForObject(ProjectMgr);
            try
            {
                var flavorCfgProvider =
                    (IVsProjectFlavorCfgProvider)
                        Marshal.GetTypedObjectForIUnknown(projectUnknown, typeof (IVsProjectFlavorCfgProvider));
                ErrorHandler.ThrowOnFailure(flavorCfgProvider.CreateProjectFlavorCfg(this, out flavoredCfg));
                if (flavoredCfg == null)
                    throw new COMException();
            }
            finally
            {
                if (projectUnknown != IntPtr.Zero)
                    Marshal.Release(projectUnknown);
            }
            // if the flavored object support XML fragment, initialize it
            var persistXML = flavoredCfg as IPersistXMLFragment;
            if (null != persistXML)
            {
                ProjectMgr.LoadXmlFragment(persistXML, DisplayName);
            }
        }
ProjectNode