Esempio n. 1
0
        public static ImportExportModel FromXml(string xml)
        {
            var doc   = XElement.Parse(xml);
            var model = new ImportExportModel();

            foreach (var language in doc.Elements("language"))
            {
                var ci = new CultureInfo((string)language.Attribute("name"));

                var languageModel = new ImportExportModelLanguage(ci);

                foreach (var resourceSet in language.Elements("resourceSet"))
                {
                    var resourceSetModel = new ImportExportModelResourceSet
                    {
                        Name = resourceSet.Attribute("name") == null ? null : (string)resourceSet.Attribute("name")
                    };

                    foreach (var resource in resourceSet.Elements("resource"))
                    {
                        var resourceModel = new ImportExportModelResource
                        {
                            Key   = (string)resource.Attribute("key"),
                            Type  = (ResourceType)Enum.Parse(typeof(ResourceType), (string)resource.Attribute("type")),
                            Value = (string)resource.Attribute("value")
                        };

                        resourceSetModel.Resources.Add(resourceModel.Key, resourceModel);
                    }

                    languageModel.ResourceSets.Add(resourceSetModel);
                }

                model.Languages.Add(languageModel.Culture, languageModel);
            }

            return(model);
        }
        public ImportExportModel Export()
        {
            var model = new ImportExportModel();

            using (var data = new DataConnection())
            {
                foreach (var language in _languages)
                {
                    var ci = new CultureInfo(language);

                    var modelLanguage = new ImportExportModelLanguage(ci);

                    foreach (var resourceSet in _resourceSets)
                    {
                        var modelResourceSet = new ImportExportModelResourceSet
                        {
                            Name = String.IsNullOrEmpty(resourceSet) ? null : resourceSet
                        };

                        var keys = from key in data.Get <IResourceKey>()
                                   where key.ResourceSet == (resourceSet == String.Empty ? null : resourceSet)
                                   select key;

                        var query = from key in keys
                                    join value in data.Get <IResourceValue>() on key.Id equals value.KeyId
                                    where value.Culture == ci.Name
                                    select new
                        {
                            Key   = key.Key,
                            Type  = key.Type,
                            Value = value.Value
                        };

                        if (!String.IsNullOrEmpty(_ns))
                        {
                            query = from o in query
                                    where o.Key.StartsWith(_ns)
                                    select o;
                        }

                        foreach (var o in query)
                        {
                            var resourceModel = new ImportExportModelResource
                            {
                                Key   = o.Key,
                                Value = o.Value
                            };

                            if (!String.IsNullOrEmpty(o.Type))
                            {
                                resourceModel.Type = (ResourceType)Enum.Parse(typeof(ResourceType), o.Type);
                            }

                            modelResourceSet.Resources.Add(resourceModel.Key, resourceModel);
                        }

                        modelLanguage.ResourceSets.Add(modelResourceSet);
                    }

                    model.Languages.Add(ci, modelLanguage);
                }
            }

            return(model);
        }