コード例 #1
0
        public static PublicConfigFile LoadFromData(string data)
        {
            var stream = data.ToStream();
            var doc    = new XmlDocument();

            using (stream)
            {
                using (var tr = new XmlTextReader(stream))
                {
                    tr.Namespaces = false;
                    doc.Load(tr);
                }
            }

            if (!doc.HasChildNodes)
            {
                throw new ConfigFileParseException();
            }

            if (!ConfigFileXmlHelper.TryGetAddress(doc, out var address))
            {
                throw new ConfigFileParseException();
            }
            if (!ConfigFileXmlHelper.TryGetSyncMode(doc, out var syncMode))
            {
                throw new ConfigFileParseException();
            }

            var severity = ConfigFileXmlHelper.GetSeverity(doc);

            var extensions = MetadataXmlHelper.GetExtensions(doc, $"{RootNodeName}/{ExtensionsListNodeName}/{MetadataConstants.ExtensionMetadataXmlNodeName}");

            return(new PublicConfigFile(address, syncMode, severity, extensions));
        }
コード例 #2
0
        public void WriteToFile(string filePath)
        {
            using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                var xmlDocument = new XmlDocument();

                var root = xmlDocument.CreateElement(string.Empty, ConfigFileXmlHelper.RootNodeName, string.Empty);
                xmlDocument.AppendChild(root);


                ConfigFileXmlHelper.CreateConfigElements(ref root, SyncMode, RepoAddress, AnalysisSeverity);

                var extensionList = xmlDocument.CreateElement(ConfigFileXmlHelper.ExtensionsListNodeName);
                foreach (var analyzer in Extensions)
                {
                    var analyzerNode = analyzer.ToXmlNode(xmlDocument);
                    extensionList.AppendChild(analyzerNode);
                }
                root.AppendChild(extensionList);

                fs.Seek(0, SeekOrigin.Begin);
                xmlDocument.Save(fs);
            }
        }