示例#1
0
        public bool Save(string fileName, ref string error)
        {
            string tempFileName = Path.GetTempFileName();

            try
            {
                using (XmlWriter writer = XmlWriter.Create(tempFileName, new XmlWriterSettings()
                {
                    Indent = true, Encoding = Encoding.Unicode
                }))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Root");
                    writer.Flush();
                    ModelSystemStructure.Save(writer);
                    if (Description != null)
                    {
                        writer.WriteStartElement("Description");
                        writer.WriteString(Description);
                        writer.WriteEndElement();
                    }
                    if (LinkedParameters != null)
                    {
                        foreach (var lp in LinkedParameters)
                        {
                            writer.WriteStartElement("LinkedParameter");
                            writer.WriteAttributeString("Name", lp.Name);
                            if (lp.Value != null)
                            {
                                writer.WriteAttributeString("Value", lp.Value.ToString());
                            }
                            foreach (var reference in lp.Parameters)
                            {
                                writer.WriteStartElement("Reference");
                                writer.WriteAttributeString("Name", LookupName(reference));
                                writer.WriteEndElement();
                            }
                            writer.WriteEndElement();
                        }
                    }
                    writer.WriteEndDocument();
                }
            }
            catch (Exception e)
            {
                Description = string.Empty;
                error       = e.Message;
                return(false);
            }
            File.Copy(tempFileName, fileName, true);
            File.Delete(tempFileName);
            return(true);
        }
示例#2
0
        /// <summary>
        /// Return a representation of this module
        /// </summary>
        public string CopyModule()
        {
            var children = GetAllChildren();

            using (MemoryStream backing = new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(backing, Encoding.Unicode))
                {
                    writer.Formatting = Formatting.Indented;
                    writer.WriteStartElement("CopiedModule");
                    writer.WriteStartElement("CopiedModules");
                    RealModelSystemStructure.Save(writer);
                    writer.WriteEndElement();
                    writer.WriteStartElement("LinkedParameters");
                    foreach (var linkedParameter in GetLinkedParameters(children))
                    {
                        writer.WriteStartElement("LinkedParameter");
                        writer.WriteAttributeString("Name", linkedParameter.Name);
                        writer.WriteAttributeString("Value", linkedParameter.GetValue());
                        foreach (var link in linkedParameter.GetParameters())
                        {
                            var match = children.FirstOrDefault(m => m.RealModelSystemStructure == link.RealParameter.BelongsTo);
                            if (match != null)
                            {
                                writer.WriteStartElement("Parameter");
                                writer.WriteAttributeString("Path", LookupName(link, this));
                                writer.WriteEndElement();
                            }
                        }
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.Flush();
                    backing.Position = 0;
                    using (var reader = new StreamReader(backing))
                    {
                        return(reader.ReadToEnd());
                    }
                }
            }
        }