Problema de serialización c#xml
Te daré una clase que escribí que usa XML para almacenar configuraciones. Vea usted mismo. Vi tu segunda pregunta. Mire esto con atención.
El almacenamiento simple está disponible
usando System;
usando System.Collections.Generic;
usando System.Text;
usando System. Xml;
usando System.IO;
usando Microsoft.Win32;
usando System.Drawing;
usando Windows. Formularios;
espacio de nombres ChartFormat
{
clase Config
{
#region Eliminar archivo de configuración p>
///
/// Eliminar archivo de configuración
///
///
public static bool DelConfig()
{
string m_strFullPath = "";
m_strFullPath = Directorio. GetCurrentDirectory() + @"\" + "Config.config";
prueba
{
File.Delete(m_strFullPath);
devuelve verdadero;
}
captura
{
devuelve falso;
}< /p >
}
#endregion
#region determina si el archivo de ruta existe y lo crea si no existe
///
/// Determinar si el archivo de ruta existe y crearlo si no existe
///
/// Ruta
ConfigFile public static void(string m_strFullPath)
{
StreamWriter sr = null;
if (!System.IO.File.Exists(m_strFullPath))
{
prueba
{
sr = nuevo StreamWriter(m_strFullPath);
sr.WriteLine("");
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("
sr.WriteLine("");
sr.WriteLine("");
sr.Close();
}
catch (Excepción e)
{
sr.Close();
MessageBox.Show("Crear ¡Error en el archivo de configuración! ", "información"
, MessageBoxButtons.OK, MessageBoxIcon.Question);
}
finalmente
{
if (sr != null) p>
{
sr.Close();
}
}
}
}
#endregion
#region Actualizar archivo de configuración
///
/// Actualizar archivo de configuración p>
///
/// Clave
/// valor
public static void UpdateConfig(string p_strKey, string p_strValue)
{
intentar
{
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config .config";
ConfigFile(m_strFullPath);
if (File.Exists(m_strFullPath))
{
//Procesamiento El archivo de datos es de solo lectura
if ((File.GetAttributes(m_strFullPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
Archivo .SetAttributes(m_strFullPath, File.GetAttributes(m_strFullPath) & (~FileAttributes.ReadOnly));
}
}
xmlDoc.Load(m_
strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn en nodeList) // Recorrer todos los nodos secundarios
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("key").IndexOf(p_strKey) != -1)
{
xe.SetAttribute("valor", p_strValue);
}
}
xmlDoc.Save( m_strFullPath);
//fs.Close();
}
catch (System.NullReferenceException NullEx)
{ p> p>
lanzar NullEx;
}
capturar (excepción ex)
{
lanzar ex; p>
}
}
#endregion
}
clase pública ConfigInfo
{< /p >
cadena privada strNameColor =ColorTranslator.ToHtml(System.Drawing.Color.Blue);
cadena privada strInfoColor = ColorTranslator.ToHtml(System.Drawing.Color.Black);
cadena privada strIsURL = "1";
cadena privada strIsEmail = "1";
cadena privada strIsMove = "1";
privada string strMoveValue = "3";
private string strIsMoveTime = "1";
///
/// Si se deben eliminar espacios< /p >
///
bool público
IsMoveTime
{
get
{
if (strIsMoveTime == "1")
{
devuelve verdadero;
}
más
{
devuelve falso;
}
}
establecer
{
si (valor)
{
strIsMoveTime = "1";
}
else
{
strIsMoveTime = "0";
}
}
}
///
/// Sangrar espacios
///
decimal público MoveValue
{
get
{
return Convertir .ToDecimal(strMoveValue);
}
establecer
{
strMoveValue = valor.ToString(); p> }
}
///
/// Si se debe sangrar
/// resumen
public bool IsMove
{
get
{
if (strIsMove == "1 " )
{
devuelve verdadero;
}
más
{
devolver falso;
}
}
establecer
{
si (valor)
{
strIsMove = "1";
}
else
{
strIsMove = "0";
}
}
}
///
/// Si se puede hacer clic en el correo electrónico
///
public bool IsEmail
{
get
{
if (strIsEmail == "1")
{
devuelve verdadero;
}
else
{
devolver falso;
}
}
establecer
{
if (valor)
{
strIsEmail = "1";
}
else
{
strIsEmail = "0";
}
}
}
///
/// Si se puede hacer clic en la página web
///
public bool IsURL
{
obtiene
{
if (strIsURL== "1")
{
devuelve verdadero;
}
más
{
devolver falso;
}
}
establecer
{
if (valor)
{
strIsURL = "1";
}
más
{
strIsURL = "0";
}
}
}
///
// / Color del mensaje
///
Color público InfoColor
{
obtener
{
return ColorTranslator.FromHtml(strInfoColor
}
set
{
strInfoColor = ColorTranslator); .ToHtml(valor);
}
}
///
/// Color de la información
///
Color público NombreColor
{
obtener
{
return ColorTranslator.FromHtml(strNameColor); ;
}
set
{
strNameColor = ColorTranslator.ToHtml(valor) ;
}
}
public ConfigInfo()
{
cadena m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Conf
ig.ConfigFile(m_strFullPath);
xmlDoc.Load(m_strFullPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes;
foreach (XmlNode xn en nodeList)//Recorre todos los nodos secundarios
{
XmlNodeReader objNdRd = new XmlNodeReader(xn);
while (objNdRd. Read())
{
if (objNdRd.GetAttribute("key").IndexOf("NameColor") != -1)
{
this.strNameColor = objNdRd.GetAttribute("valor");
}
if (objNdRd.GetAttribute("clave").IndexOf("InfoColor" ) != -1)
{
this.strInfoColor = objNdRd.GetAttribute("valor");
}
si (objNdRd.GetAttribute("key").IndexOf("IsURL") != -1)
{
this.strIsURL = objNdRd.GetAttribute("value");
}
if (objNdRd.GetAttribute("key").IndexOf("IsEmail") != -1)
{
this.strIsEmail = objNdRd.GetAttribute("valor");
}
if (objNdRd.GetAttribute("clave").IndexOf("IsMove") != -1 )
{
this.strIsMove = objNdRd.GetAttribute("valor");
}<
/p>
if (objNdRd.GetAttribute("key").IndexOf("MoveValue") != -1)
{
this.strMoveValue = objNdRd.GetAttribute ("valor");
}
}
}
}
actualización pública vacía()
{
Config.DelConfig();
string m_strFullPath = "";
XmlDocument xmlDoc = new XmlDocument();
m_strFullPath = Directory.GetCurrentDirectory() + @"\" + "Config.config";
Config.ConfigFile(m_strFullPath);
Config.UpdateConfig( "NameColor", strNameColor);
Config.UpdateConfig("InfoColour", strInfoColor);
Config.UpdateConfig("IsURL", strIsURL);
Config.UpdateConfig("IsEmail", strIsEmail);
Config.UpdateConfig("IsMove", strIsMove);
Config.UpdateConfig("MoveValue", strMoveValue);
}
}
}