object Deserialize(string name, DataNode node, Type type, DataContext ctx) { if (type.IsAssignableFrom(typeof(XmlElement))) { // The xml content is the first child of the data node DataItem it = node as DataItem; if (it == null || it.ItemData.Count > 1) { throw new InvalidOperationException("Can't convert property to an XmlElement object."); } if (it.ItemData.Count == 0) { return(null); } XmlConfigurationWriter sw = new XmlConfigurationWriter(); XmlDocument doc = new XmlDocument(); return(sw.Write(doc, it.ItemData [0])); } if (ctx == null) { throw new InvalidOperationException("Can't deserialize property '" + name + "'. Serialization context not set."); } DataSerializer ser = new DataSerializer(ctx); ser.SerializationContext.BaseFile = sourceFile; object ob = ser.Deserialize(type, node); return(ob); }
/// <summary> /// Writes a configuration using the specified encryption engine to the specified path /// </summary> /// <param name="encryptionEngine">The encryption engine to use while writing the configuration, null if no encryption is desired</param> /// <param name="configuration">The confiruration to write</param> /// <param name="path">The path to write it to</param> /// <returns></returns> public static bool WriteConfiguration(FileEncryptionEngine encryptionEngine, XmlConfiguration configuration, string path) { Stream stream = null; _lastException = null; try { if (configuration != null) { if (configuration.HasUnpersistedChanges()) { configuration.AcceptChanges(); stream = (encryptionEngine != null ? encryptionEngine.CreateEncryptorStream(path) : new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)); XmlConfigurationWriter writer = new XmlConfigurationWriter(); writer.Write(configuration, stream, false); configuration.SetHasUnpersistedChanges(false); } return(true); } } catch (Exception ex) { Log.WriteLine(ex); _lastException = ex; } finally { if (stream != null) { stream.Close(); } } return(false); }
void UpdateEntry() { var ser = new DataSerializer(new DataContext()); var data = ser.Serialize(this); XmlDocument doc = new XmlDocument(); var writer = new XmlConfigurationWriter { Namespace = entry.MSBuildProject.Namespace }; var elem = writer.Write(doc, data); entry.MSBuildProject.SetMonoDevelopProjectExtension("Deployment.LinuxDeployData", elem); }
internal void SaveToFile(StreamWriter writer) { XmlWriterSettings xws = new XmlWriterSettings(); xws.Indent = true; XmlConfigurationWriter cw = new XmlConfigurationWriter(); cw.StoreAllInElements = true; cw.StoreInElementExceptions = new String[] { "scope", "inheritsSet", "inheritsScope" }; using (XmlWriter xw = XmlTextWriter.Create(writer, xws)) { xw.WriteStartElement("PolicySet"); if (policies != null) { foreach (KeyValuePair <PolicyKey, object> policyPair in policies) { cw.Write(xw, PolicyService.DiffSerialize(policyPair.Key.PolicyType, policyPair.Value, policyPair.Key.Scope)); } } xw.WriteEndElement(); } }
internal static void WriteExternalProjectProperties(this MSBuildProject project, object ob, Type typeToScan, bool includeBaseMembers = false) { DataSerializer ser = new DataSerializer(Services.ProjectService.DataContext); var props = Services.ProjectService.DataContext.GetProperties(ser.SerializationContext, ob); XmlConfigurationWriter writer = null; foreach (var prop in props) { if (!prop.IsExternal) { continue; } var val = prop.GetValue(ob); if (val != null) { var data = prop.Serialize(ser.SerializationContext, ob, val); if (data != null) { data.Name = prop.Name; if (writer == null) { writer = new XmlConfigurationWriter { Namespace = MSBuildProject.Schema } } ; XmlDocument doc = new XmlDocument(); var elem = writer.Write(doc, data); // TODO NPM project.SetMonoDevelopProjectExtension(prop.Name, elem); continue; } } project.RemoveMonoDevelopProjectExtension(prop.Name); } }
internal void SaveToXml(XmlWriter xw) { XmlConfigurationWriter cw = new XmlConfigurationWriter(); cw.StoreAllInElements = true; cw.StoreInElementExceptions = new String[] { "scope", "inheritsSet", "inheritsScope" }; xw.WriteStartElement("PolicySet"); if (!string.IsNullOrEmpty(Name)) { xw.WriteAttributeString("name", Name); } if (!string.IsNullOrEmpty(Id)) { xw.WriteAttributeString("id", Id); } if (policies != null) { foreach (KeyValuePair <PolicyKey, object> policyPair in policies) { cw.Write(xw, PolicyService.DiffSerialize(policyPair.Key.PolicyType, policyPair.Value, policyPair.Key.Scope)); } } xw.WriteEndElement(); }
public void WriteThrowsForNullStream() { var cfgWriter = new XmlConfigurationWriter(); cfgWriter.Write(null, new Dictionary<string, string>()); } /* End of Function - WriteThrowsForNullStream */
public void WriteThrowsForNullData() { using var stream = new MemoryStream(); var cfgWriter = new XmlConfigurationWriter(); cfgWriter.Write(stream, null); } /* End of Function - WriteThrowsForNullData */
public static void WriteObjectProperties(this IPropertySet pset, object ob, Type typeToScan, bool includeBaseMembers = false) { DataSerializer ser = new DataSerializer(Services.ProjectService.DataContext); var props = Services.ProjectService.DataContext.GetProperties(ser.SerializationContext, ob); XmlConfigurationWriter cwriter = null; var mso = pset as IMSBuildProjectObject; if (mso != null && mso.ParentProject != null) { ser.SerializationContext.BaseFile = mso.ParentProject.FileName; } ser.SerializationContext.DirectorySeparatorChar = '\\'; foreach (var prop in props) { if (prop.IsExternal) { continue; } bool merge = Attribute.IsDefined(prop.Member, typeof(MergeToProjectAttribute)); if (prop.PropertyType == typeof(FilePath)) { var val = (FilePath)prop.GetValue(ob); FilePath def = (string)prop.DefaultValue; pset.SetValue(prop.Name, val, def, mergeToMainGroup: merge); } else if (prop.DataType is PathDataType && prop.PropertyType == typeof(string)) { FilePath val = (string)prop.GetValue(ob); FilePath def = (string)prop.DefaultValue; pset.SetValue(prop.Name, val, def, mergeToMainGroup: merge); } else if (prop.PropertyType == typeof(string)) { pset.SetValue(prop.Name, (string)prop.GetValue(ob), (string)prop.DefaultValue, merge); } else if (prop.DataType.IsSimpleType) { pset.SetValue(prop.Name, prop.GetValue(ob), prop.DefaultValue, merge); } else if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { pset.SetValue(prop.Name, prop.GetValue(ob), prop.DefaultValue, merge); } else { var val = prop.GetValue(ob); if (val != null) { if (cwriter == null) { cwriter = new XmlConfigurationWriter { Namespace = MSBuildProject.Schema } } ; var w = new StringWriter(); var data = prop.Serialize(ser.SerializationContext, ob, val); if (data != null) { cwriter.Write(new XmlTextWriter(w), data); pset.SetValue(prop.Name, w.ToString()); } else { pset.RemoveProperty(prop.Name); } } else { pset.RemoveProperty(prop.Name); } } } }