protected virtual void WriteToXmlElement(System.Xml.XmlElement xmlElement, PersistenceFlags flags)
        {
            //settings
            System.Xml.XmlNode xmlsettings = xmlElement.SelectSingleNode("settings");
            if (xmlsettings == null)
            {
                xmlsettings = xmlElement.OwnerDocument.CreateElement("settings");
                xmlElement.AppendChild(xmlsettings);
            }
            //items
            System.Xml.XmlNode xmlitems = xmlsettings.SelectSingleNode("items");
            if (xmlitems == null)
            {
                xmlitems = xmlsettings.OwnerDocument.CreateElement("items");
                xmlsettings.AppendChild(xmlitems);
                xmlitems.Attributes.Append(xmlitems.OwnerDocument.CreateAttribute("schemaversion"));
                xmlitems.Attributes["schemaversion"].Value = "1";
            }

            foreach (PersistableItem item in m_Dictionary.Values)
            {
                string             xPath   = string.Format("item[@name='{0}' and @type='{1}' and @schemaversion='1']", item.Name, item.Type.FullName);
                System.Xml.XmlNode xmlitem = xmlitems.SelectSingleNode(xPath);

                if ((flags & PersistenceFlags.OnlyChanges) == PersistenceFlags.OnlyChanges &&
                    item.IsChanged == false)
                {                 //remove item
                    if (xmlitem != null)
                    {
                        xmlitems.RemoveChild(xmlitem);
                    }
                }
                else                 //add item
                {
                    if (xmlitem == null)
                    {
                        xmlitem = xmlitems.OwnerDocument.CreateElement("item");
                        xmlitems.AppendChild(xmlitem);

                        xmlitem.Attributes.Append(xmlitem.OwnerDocument.CreateAttribute("name"));
                        xmlitem.Attributes.Append(xmlitem.OwnerDocument.CreateAttribute("type"));
                        xmlitem.Attributes.Append(xmlitem.OwnerDocument.CreateAttribute("schemaversion"));

                        xmlitem.Attributes["name"].Value          = item.Name;
                        xmlitem.Attributes["type"].Value          = item.Type.FullName;
                        xmlitem.Attributes["schemaversion"].Value = "1";
                    }
                    xmlitem.InnerText = item.Validator.ValueToString(item.Value);
                }
            }
        }
示例#2
0
        internal IntermediateFormatWriter(Stream str, long startOffset, List <Declaration> declarations, PersistenceHelper persistenceContext, int compatVersion, bool prohibitSerializableValues)
        {
            m_writer                     = new PersistenceBinaryWriter(str);
            m_writtenDecls               = new Dictionary <ObjectType, Declaration>(EqualityComparers.ObjectTypeComparerInstance);
            m_currentDeclaration         = null;
            m_currentMemberIndex         = 0;
            m_lastMemberInfoIndex        = 0;
            m_currentMember              = null;
            m_persistenceContext         = persistenceContext;
            m_isSeekable                 = false;
            m_binaryFormatter            = null;
            m_compatVersion              = compatVersion;
            m_prohibitSerializableValues = prohibitSerializableValues;
            if (startOffset == 0L)
            {
                Global.Tracer.Assert(!m_isSeekable, "(!m_isSeekable)");
                Write(IntermediateFormatVersion.Current);
            }
            m_isSeekable = (declarations != null);
            PersistenceFlags persistenceFlags = PersistenceFlags.None;

            if (m_isSeekable)
            {
                persistenceFlags = PersistenceFlags.Seekable;
            }
            if (UsesCompatVersion)
            {
                persistenceFlags |= PersistenceFlags.CompatVersioned;
            }
            if (startOffset == 0L)
            {
                m_writer.WriteEnum((int)persistenceFlags);
                if (UsesCompatVersion)
                {
                    ((BinaryWriter)m_writer).Write(m_compatVersion);
                }
                if (m_isSeekable)
                {
                    WriteDeclarations(declarations);
                }
            }
            else if (m_isSeekable)
            {
                FilterAndStoreDeclarations(declarations);
            }
        }
		protected virtual void WriteToIsolatedStorage(string fileName, PersistenceFlags flags)
		{
			using (System.IO.IsolatedStorage.IsolatedStorageFile l_Storage = GetStorage())
			{
				System.IO.IsolatedStorage.IsolatedStorageFileStream l_File = null;
				l_File = new System.IO.IsolatedStorage.IsolatedStorageFileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, l_Storage);
				try
				{
					WriteToStream(l_File, flags);
				}
				finally
				{
					l_File.Close();
				}

				l_Storage.Close();
			}		
		}
        protected virtual void WriteToIsolatedStorage(string fileName, PersistenceFlags flags)
        {
            using (System.IO.IsolatedStorage.IsolatedStorageFile l_Storage = GetStorage())
            {
                System.IO.IsolatedStorage.IsolatedStorageFileStream l_File = null;
                l_File = new System.IO.IsolatedStorage.IsolatedStorageFileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, l_Storage);
                try
                {
                    WriteToStream(l_File, flags);
                }
                finally
                {
                    l_File.Close();
                }

                l_Storage.Close();
            }
        }
        protected virtual void WriteToStream(System.IO.Stream stream, PersistenceFlags flags)
        {
//			System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.UTF8);
//
//			writer.Formatting = System.Xml.Formatting.Indented;
//			writer.WriteStartDocument();
//			writer.WriteStartElement("settings");
//
//			writer.WriteStartElement("items");
//			writer.WriteAttributeString("schemaversion", "1");
//
//			foreach (PersistableItem item in m_Dictionary.Values)
//			{
//				if ( (flags & PersistenceFlags.OnlyChanges) == PersistenceFlags.OnlyChanges &&
//					item.IsChanged == false)
//					continue;
//
//				writer.WriteStartElement("item");
//				writer.WriteAttributeString("name", item.Name);
//				writer.WriteAttributeString("type", item.Type.FullName);
//				writer.WriteAttributeString("schemaversion", "1");
//				writer.WriteString(item.Validator.ValueToString(item.Value));
//				writer.WriteEndElement();
//			}
//
//			writer.WriteEndElement();
//
//			writer.WriteEndElement();
//			writer.WriteEndDocument();
//			writer.Flush();


            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.AppendChild(doc.CreateElement("settings"));
            WriteToXmlElement(doc.DocumentElement, flags);

            System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.UTF8);
            writer.Formatting = System.Xml.Formatting.Indented;
            doc.WriteTo(writer);
            writer.Flush();
        }
		protected virtual void WriteToXmlElement(System.Xml.XmlElement xmlElement, PersistenceFlags flags)
		{
			//settings
			System.Xml.XmlNode xmlsettings = xmlElement.SelectSingleNode("settings");
			if (xmlsettings == null)
			{
				xmlsettings = xmlElement.OwnerDocument.CreateElement("settings");
				xmlElement.AppendChild(xmlsettings);
			}
			//items
			System.Xml.XmlNode xmlitems = xmlsettings.SelectSingleNode("items");
			if (xmlitems == null)
			{
				xmlitems = xmlsettings.OwnerDocument.CreateElement("items");
				xmlsettings.AppendChild(xmlitems);
				xmlitems.Attributes.Append(xmlitems.OwnerDocument.CreateAttribute("schemaversion"));
				xmlitems.Attributes["schemaversion"].Value = "1";
			}

			foreach (PersistableItem item in m_Dictionary.Values)
			{
				string xPath = string.Format("item[@name='{0}' and @type='{1}' and @schemaversion='1']", item.Name, item.Type.FullName);
				System.Xml.XmlNode xmlitem = xmlitems.SelectSingleNode(xPath);

				if ( (flags & PersistenceFlags.OnlyChanges) == PersistenceFlags.OnlyChanges &&
					item.IsChanged == false)
				{ //remove item
					if (xmlitem != null)
						xmlitems.RemoveChild(xmlitem);
				}
				else //add item
				{
					if (xmlitem == null)
					{
						xmlitem = xmlitems.OwnerDocument.CreateElement("item");
						xmlitems.AppendChild(xmlitem);

						xmlitem.Attributes.Append(xmlitem.OwnerDocument.CreateAttribute("name"));
						xmlitem.Attributes.Append(xmlitem.OwnerDocument.CreateAttribute("type"));
						xmlitem.Attributes.Append(xmlitem.OwnerDocument.CreateAttribute("schemaversion"));
						
						xmlitem.Attributes["name"].Value = item.Name;
						xmlitem.Attributes["type"].Value = item.Type.FullName;
						xmlitem.Attributes["schemaversion"].Value = "1";
					}
					xmlitem.InnerText = item.Validator.ValueToString(item.Value);
				}
			}
		}
		protected virtual void WriteToStream(System.IO.Stream stream, PersistenceFlags flags)
		{
//			System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.UTF8);
//			
//			writer.Formatting = System.Xml.Formatting.Indented;
//			writer.WriteStartDocument();
//			writer.WriteStartElement("settings");
//
//			writer.WriteStartElement("items");
//			writer.WriteAttributeString("schemaversion", "1");
//			
//			foreach (PersistableItem item in m_Dictionary.Values)
//			{
//				if ( (flags & PersistenceFlags.OnlyChanges) == PersistenceFlags.OnlyChanges &&
//					item.IsChanged == false)
//					continue;
//
//				writer.WriteStartElement("item");
//				writer.WriteAttributeString("name", item.Name);
//				writer.WriteAttributeString("type", item.Type.FullName);
//				writer.WriteAttributeString("schemaversion", "1");
//				writer.WriteString(item.Validator.ValueToString(item.Value));
//				writer.WriteEndElement();
//			}
//
//			writer.WriteEndElement();
//
//			writer.WriteEndElement();
//			writer.WriteEndDocument();
//			writer.Flush();


			System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
			doc.AppendChild(doc.CreateElement("settings"));
			WriteToXmlElement(doc.DocumentElement, flags);

			System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.UTF8);
			writer.Formatting = System.Xml.Formatting.Indented;
			doc.WriteTo(writer);
			writer.Flush();
		}