本文整理汇总了C#中System.Xml.Serialization.XmlSerializer.Serialize方法的典型用法代码示例。如果您正苦于以下问题:C# System.Xml.Serialization.XmlSerializer.Serialize方法的具体用法?C# System.Xml.Serialization.XmlSerializer.Serialize怎么用?C# System.Xml.Serialization.XmlSerializer.Serialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.XmlSerializer
的用法示例。
在下文中一共展示了System.Xml.Serialization.XmlSerializer.Serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ObjetoSerializado
public static string ObjetoSerializado(Object Objeto)
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(Objeto.GetType());
System.IO.StringWriter textWriter = new System.IO.StringWriter();
x.Serialize(textWriter, Objeto);
return textWriter.ToString();
}
示例2: Write
public static void Write(string file, AltaCache overview)
{
if (string.IsNullOrEmpty(file))
throw new Exception("File Not Empty");
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(AltaCache));
System.Xml.XmlWriterSettings setting = new System.Xml.XmlWriterSettings();
setting.Encoding = Encoding.UTF8;
setting.CloseOutput = true;
setting.NewLineChars = "\r\n";
setting.Indent = true;
if (!File.Exists(file))
{
using (Stream s = File.Open(file, FileMode.OpenOrCreate))
{
System.Xml.XmlWriter tmp = System.Xml.XmlWriter.Create(s, setting);
writer.Serialize(tmp, overview);
}
}
else
{
using (Stream s = File.Open(file, FileMode.Truncate))
{
System.Xml.XmlWriter tmp = System.Xml.XmlWriter.Create(s, setting);
writer.Serialize(tmp, overview);
}
}
}
示例3: Button_ClickSave
private void Button_ClickSave(object sender, RoutedEventArgs e)
{
//WIP
string CartBox = ArtBox.SelectedItems.ToString();
string Comboselectedmonth = DateBoxMonth.SelectedItem.ToString();
string Comboselectedyear = DateBoxYear.SelectedItem.ToString();
string Agynentxt = GyneBox.Text.ToString();
string Aworkertxt = WorkerBox.Text.ToString();
string Asoldiertxt = SoldierBox.Text.ToString();
Colony ColonyOne = new Colony();
ColonyOne.Cname = NameBox.Text;
ColonyOne.Cart = CartBox;
ColonyOne.Cnote = NoteBox.Text;
ColonyOne.Cgdatummonth = Comboselectedmonth;
ColonyOne.Cgdatumyear = Comboselectedyear;
ColonyOne.Agynen = Agynentxt;
ColonyOne.Aworker = Aworkertxt;
ColonyOne.Asoldiers = Asoldiertxt;
//nn to check if folder exists, "System.IO.Directory.CreateDirectory" does that for us
string mydocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
System.IO.Directory.CreateDirectory(mydocs + "/AntManager");
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(Colony));
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/AntManager/AntManagerSaveFile.xml";
System.IO.FileStream file = System.IO.File.Create(path);
writer.Serialize(file, ColonyOne);
file.Close();
}
示例4: testBasicStructure
public void testBasicStructure()
{
Being b = new Being();
b.Ident = "ALASTAIR";
b.RightEye = new BeingEye();
b.RightEye.EyeState = EyeState.FIXATION;
b.RightEye.GazeVector = new Pose6D();
CommunicationHelper.setPose6D( b.RightEye.GazeVector, 23.5678549M, 92.64654864M, null, 45.765234560M, 60.34444124365M, null, 0.9M );
GazeTrackData gt = new GazeTrackData();
gt.Timestamp = DateTime.UtcNow;
gt.SequenceNumber = 0;
System.Collections.Generic.List<Being> beingList = new System.Collections.Generic.List<Being>();
beingList.Add( b );
gt.Beings = beingList.ToArray();
System.Collections.Generic.List<GazeTrackData> gazeTrackDataList = new System.Collections.Generic.List<GazeTrackData>();
gazeTrackDataList.Add( gt );
GazeTrackDataSet gtd = new GazeTrackDataSet();
gtd.GazeTrackData = gazeTrackDataList.ToArray();
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer( gtd.GetType() );
TextWriter writer = new SpecificEncodingStringWriter( Encoding.UTF8 );
XmlWriter xmlWriter = XmlWriter.Create( writer );
x.Serialize( xmlWriter, gtd );
Console.WriteLine( "XML:" );
Console.WriteLine( writer.ToString() );
Console.WriteLine( "Length: " + writer.ToString().Length );
log.Debug( "Generated XML" );
log.Debug( writer.ToString() );
log.Debug( "Generated XML Length: " + writer.ToString().Length );
}
示例5: Save
public void Save()
{
var reader = new System.Xml.Serialization.XmlSerializer(typeof(List<Words>));
var file = new FileStream("words.xml", FileMode.Create);
reader.Serialize(file, LocalWords._words);
file.Close();
}
示例6: BackupVendedorLinkButton_Click
protected void BackupVendedorLinkButton_Click(object sender, EventArgs e)
{
try
{
if (CedWebRN.Fun.NoEstaLogueadoUnUsuarioPremium((CedWebEntidades.Sesion)Session["Sesion"]))
{
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Esta funcionalidad es exclusiva del SERVICIO PREMIUM. Contáctese con Cedeira Software Factory para acceder al servicio.');</script>");
}
else
{
CedWebEntidades.Vendedor vendedor = new CedWebEntidades.Vendedor();
vendedor.IdCuenta = ((CedWebEntidades.Sesion)Session["Sesion"]).Cuenta.Id;
CedWebRN.Vendedor.Leer(vendedor, (CedEntidades.Sesion)Session["Sesion"]);
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(vendedor.GetType());
System.IO.MemoryStream m = new System.IO.MemoryStream();
System.Xml.XmlWriter writerdememoria = new System.Xml.XmlTextWriter(m, System.Text.Encoding.GetEncoding("ISO-8859-1"));
x.Serialize(writerdememoria, vendedor);
m.Seek(0, System.IO.SeekOrigin.Begin);
string nombreArchivo = "eFact-Vendedor-" + ((CedWebEntidades.Sesion)Session["Sesion"]).Cuenta.Id.Replace(".", String.Empty).ToUpper() + ".xml";
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(@"~/Temp/" + nombreArchivo), System.IO.FileMode.Create);
m.WriteTo(fs);
fs.Close();
Server.Transfer("~/DescargaTemporarios.aspx?archivo=" + nombreArchivo, false);
}
}
catch (Microsoft.ApplicationBlocks.ExceptionManagement.Validaciones.ElementoInexistente)
{
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('No hay datos del Vendedor para descargar.');</script>");
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.Message.ToString() + "');</script>");
}
}
示例7: BackupCompradoresLinkButton_Click
protected void BackupCompradoresLinkButton_Click(object sender, EventArgs e)
{
if (CedWebRN.Fun.NoEstaLogueadoUnUsuarioPremium((CedWebEntidades.Sesion)Session["Sesion"]))
{
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Esta funcionalidad es exclusiva del SERVICIO PREMIUM. Contáctese con Cedeira Software Factory para acceder al servicio.');</script>");
}
else
{
List<CedWebEntidades.Comprador> compradores = CedWebRN.Comprador.Lista(((CedWebEntidades.Sesion)Session["Sesion"]).Cuenta, (CedEntidades.Sesion)Session["Sesion"], false);
if (compradores.Count == 0)
{
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('No hay datos de Compradores para descargar.');</script>");
}
else
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(compradores.GetType());
System.IO.MemoryStream m = new System.IO.MemoryStream();
System.Xml.XmlWriter writerdememoria = new System.Xml.XmlTextWriter(m, System.Text.Encoding.GetEncoding("ISO-8859-1"));
x.Serialize(writerdememoria, compradores);
m.Seek(0, System.IO.SeekOrigin.Begin);
string nombreArchivo = "eFact-Compradores-" + ((CedWebEntidades.Sesion)Session["Sesion"]).Cuenta.Id.Replace(".", String.Empty).ToUpper() + ".xml";
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(@"~/Temp/" + nombreArchivo), System.IO.FileMode.Create);
m.WriteTo(fs);
fs.Close();
Server.Transfer("~/DescargaTemporarios.aspx?archivo=" + nombreArchivo, false);
}
}
}
示例8: Serialize
public static void Serialize(object obj, TextWriter output)
{
Debug.Assert(obj != null);
Debug.Assert(output != null);
#if !NET_1_0 && !NET_1_1
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
settings.CheckCharacters = false;
settings.OmitXmlDeclaration = true;
XmlWriter writer = XmlWriter.Create(output, settings);
#else
XmlTextWriter writer = new XmlTextWriter(output);
writer.Formatting = Formatting.Indented;
#endif
try
{
SystemXmlSerializer serializer = new SystemXmlSerializer(obj.GetType());
serializer.Serialize(writer, obj);
writer.Flush();
}
finally
{
writer.Close();
}
}
示例9: Logout
public static void Logout(User user)
{
// The following is just test code
//User user = new User("Ted", "Password", 5000.00);
//user.Holdings.Add(new Holding("GOOG"));
//DateTime newDate = new DateTime();
//user.WatchList.Add(new Company("test", "GOOG", "Something", "Something", "Something", 50.00));
//user.Transactions.Add(new Transaction("Test", "Test", 50.00, 50, DateTime.Now));
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(User));
//Construct the filepath
string filepath = Directory.GetCurrentDirectory() + "\\" + user.Username + ".txt";
// Clear the file
if (File.Exists(filepath) )
{
File.WriteAllText(filepath, string.Empty);
}
System.IO.StreamWriter file = new System.IO.StreamWriter(
filepath);
writer.Serialize(file, user);
file.Close();
}
示例10: WriteXML
public static void WriteXML()
{
System.IO.FileStream file;
//Book overview = new Book();
//overview.title = "Serialization Overview";
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(CountItem));
//var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//wc.xml";
string path = "wc.xml";
try
{
file = System.IO.File.Create(path);
}
catch (Exception e)
{
MessageBox.Show("Error "+e.ToString());
throw;
}
try
{
writer.Serialize(file, countitems);
}
catch (Exception e)
{
MessageBox.Show("Error " + e.ToString());
throw;
}
file.Close();
}
示例11: Main
static void Main()
{
/// <summary>
/// XML設定ファイル
/// </summary>
const string FILE_NAME = @"homeAppManager.xml";
try
{
using (System.IO.TextReader reader = new System.IO.StreamReader(FILE_NAME, Encoding.UTF8, false))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Config));
Config.init = (Config)serializer.Deserialize(reader);
}
}
catch (System.IO.FileNotFoundException ex)
{
//XmlSerializerオブジェクトを作成
using (System.IO.TextWriter writer = new System.IO.StreamWriter(FILE_NAME, false, Encoding.UTF8))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(Config.init.GetType());
serializer.Serialize(writer, Config.init);
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frm_Main());
}
示例12: SaveXml
/// <summary>
/// 序列化
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="obj">要序列化的实体类</param>
/// <param name="type">类型</param>
public static void SaveXml(string filePath, object obj, System.Type type)
{
//////序列化 成 XML 文件 Object -- > XML
//如果文件存在 则先删除原来文件
if (System.IO.File.Exists(filePath))
File.Delete(filePath);
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
try
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
catch (Exception e)
{
e = e;
throw;
}
}
////////序列化 成 2进制 文件
//FileStream fs = new FileStream(filePath, FileMode.Create);
//BinaryFormatter bf = new BinaryFormatter();
//bf.Serialize(fs, obj);
}
示例13: Serialize
public static string Serialize(object oObject, bool Indent = false)
{
System.Xml.Serialization.XmlSerializer oXmlSerializer = null;
System.Text.StringBuilder oStringBuilder = null;
System.Xml.XmlWriter oXmlWriter = null;
string sXML = null;
System.Xml.XmlWriterSettings oXmlWriterSettings = null;
System.Xml.Serialization.XmlSerializerNamespaces oXmlSerializerNamespaces = null;
// -----------------------------------------------------------------------------------------------------------------------
// Lage XML
// -----------------------------------------------------------------------------------------------------------------------
oStringBuilder = new System.Text.StringBuilder();
oXmlSerializer = new System.Xml.Serialization.XmlSerializer(oObject.GetType());
oXmlWriterSettings = new System.Xml.XmlWriterSettings();
oXmlWriterSettings.OmitXmlDeclaration = true;
oXmlWriterSettings.Indent = Indent;
oXmlWriter = System.Xml.XmlWriter.Create(new System.IO.StringWriter(oStringBuilder), oXmlWriterSettings);
oXmlSerializerNamespaces = new System.Xml.Serialization.XmlSerializerNamespaces();
oXmlSerializerNamespaces.Add(string.Empty, string.Empty);
oXmlSerializer.Serialize(oXmlWriter, oObject, oXmlSerializerNamespaces);
oXmlWriter.Close();
sXML = oStringBuilder.ToString();
return sXML;
}
示例14: ConvertToXML
public void ConvertToXML(string path)
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(this.GetType());
System.IO.StreamWriter writer = new System.IO.StreamWriter(path);
x.Serialize(writer, this);
}
示例15: SerializationTest
public void SerializationTest()
{
config.osFamily = 3;
System.Xml.Serialization.XmlSerializer serializaer = new System.Xml.Serialization.XmlSerializer(config.GetType());
serializaer.Serialize(Console.Out, config);
Console.WriteLine();
}