private void btXMLWrite_Click(object sender, System.EventArgs e)
{
try
{
xw = new XmlTextWriter(tbFile.Text, new System.Text.UTF8Encoding(false));
xw.Formatting = Formatting.Indented;
xw.WriteStartElement("Configuracion");
xw.WriteAttributeString("Game","FMC");
SaveVideo();
SaveAudio();
xw.WriteEndElement();
xw.Close();
}
catch(XmlException ex)
{
MessageBox.Show(ex.ToString(),"Error de Xml");
}
catch(System.ArgumentException ea)
{
MessageBox.Show("El nombre de archivo indicado no es válido."+(char)13+"Error devuelto: "+ea.ToString(),"Error al crear XML");
}
finally
{
xw.Close();
}
}
private void SaveVideo()
{
xw.WriteStartElement("Video");
try
{
xw.WriteElementString("VideoMode",cbVideoMode.SelectedItem.ToString());
}
catch(System.NullReferenceException)
{
xw.WriteElementString("VideoMode","No especificado");
}
try
{
xw.WriteElementString("ColorDepth",XmlConvert.ToString(Int32.Parse(cbColorDepth.SelectedItem.ToString())));
}
catch(System.NullReferenceException)
{
xw.WriteElementString("ColorDepth","No especificado");
}
xw.WriteEndElement();
}
private void SaveAudio()
{
xw.WriteStartElement("Audio");
try
{
xw.WriteElementString("SoundMode",cbSndMode.SelectedItem.ToString());
}
catch(System.NullReferenceException)
{
xw.WriteElementString("SoundMode","No especificado");
}
try
{
xw.WriteElementString("SoundVol",XmlConvert.ToString(tbVolumen.Value));
}
catch(System.NullReferenceException)
{
xw.WriteElementString("SoundVol","No especificado");
}
xw.WriteEndElement();
}
private void btXMLRead_Click(object sender, System.EventArgs e)
{
try
{
xr = new XmlTextReader(tbFile.Text);
if(xr.Read()==true)
{
if(xr.NodeType==XmlNodeType.Element && xr.Name == "Configuracion")
{
xr.MoveToFirstAttribute();
if(xr.Name == "Game" && xr.Value == "FMC")
{
while(xr.Read())
{
if(xr.NodeType == XmlNodeType.Element &&
(xr.Name == "Video" || xr.Name == "Audio"))
{
if(xr.Name == "Video")
{
LoadVideo();
continue;
}
else
{
LoadAudio();
continue;
}
break;
}
}
}
}
}
xr.Close();
}
catch(Exception ex)
{
MessageBox.Show("El nombre de archivo indicado no es válido."+(char)13+"Error devuelto: "+ex.ToString(),"Error al cargar XML");
}
finally
{
xr.Close();
}
}
private void LoadVideo()
{
String str;
byte c =0;
while(xr.Read())
{
if(xr.NodeType == XmlNodeType.Element)
{
switch(xr.Name)
{
case "VideoMode":
str = xr.ReadString();
if(str == "FullScreen" || str == "Windowed")
cbVideoMode.SelectedItem = str;
c++;
break;
case "ColorDepth":
str = xr.ReadString();
if(str == "24" || str == "32" || str == "16")
cbColorDepth.SelectedItem = XmlConvert.ToInt32(str).ToString();
c++;
break;
}
}
if(c==2)
break;
}
}
private void LoadAudio()
{
String str;
byte v=0, c=0;
while(xr.Read())
{
if(xr.NodeType == XmlNodeType.Element)
{
switch(xr.Name)
{
case "SoundMode":
str = xr.ReadString();
if(str == "Stereo" || str == "MonoAural" || str == "Surround")
cbSndMode.SelectedItem = str;
c++;
break;
case "SoundVol":
v =(byte) XmlConvert.ToInt32(xr.ReadString());
if(v >= 0 && v <= 255)
{
tbVolumen.Value =(int) v;
lbVolIndicator.Text = v.ToString();
}
c++;
break;
}
}
if(c==2)
break;
}
}