C# 列表<&燃气轮机;存储流转换
嗨,我是C#新手,我想将列表类型转换为MemoryStream:C# 列表<&燃气轮机;存储流转换,c#,stream,C#,Stream,嗨,我是C#新手,我想将列表类型转换为MemoryStream: List<String> listType=new List<String>; MemoryStream ms=new MemoryStream(); 它显示出可疑的演员阵容。 请帮忙 如果序列化是您想要的,那么它是xml还是json 它们在性能方面几乎相同: 下面是一个很好的例子: 和实际序列化 private void SerializeList() { // Create an instan
List<String> listType=new List<String>;
MemoryStream ms=new MemoryStream();
它显示出可疑的演员阵容。
请帮忙 如果序列化是您想要的,那么它是xml还是json 它们在性能方面几乎相同: 下面是一个很好的例子: 和实际序列化
private void SerializeList()
{
// Create an instance of the EmployeeList class
EmployeeList employeeList = new EmployeeList();
// Create a few instances of the Employee class
Employee emp1 = new Employee();
emp1.Name = "John";
emp1.Surname = "Smith";
emp1.DateOfBirth = new DateTime(1980, 10, 08);
emp1.Sex = Employee.EmployeeSex.Male;
emp1.Position = "Software Engineer";
Employee emp2 = new Employee();
emp2.Name = "David";
emp2.Surname = "McGregor";
emp2.DateOfBirth = new DateTime(1973, 01, 13);
emp2.Sex = Employee.EmployeeSex.Male;
emp2.Position = "Product Manager";
Employee emp3 = new Employee();
emp3.Name = "Sarah";
emp3.Surname = "Crow";
emp3.DateOfBirth = new DateTime(1983, 11, 23);
emp3.Sex = Employee.EmployeeSex.Female;
emp3.Position = "Software Tester";
// Add the employees to the list
employeeList.AddEmployee(emp1);
employeeList.AddEmployee(emp2);
employeeList.AddEmployee(emp3);
// Create an instance of System.Xml.Serialization.XmlSerializer
XmlSerializer serializer = new XmlSerializer(employeeList.GetType());
// Create an instance of System.IO.TextWriter
// to save the serialized object to disk
TextWriter textWriter = new StreamWriter("C:\\Employee\\employeeList.xml");
// Serialize the employeeList object
serializer.Serialize(textWriter, employeeList);
// Close the TextWriter
textWriter.Close();
}
无法将列表转换为内存流。这是两种完全不同的类型。你想把它序列化吗?如果是这样的话,您是否关心它的序列化程度(JSON、XML、二进制)?因此列表是。。。在这里是字符串的列表,
MemoryStream
是一个字节的数组。你想如何把它们变魔术。你需要发明一些规则,只有知道这些规则,我们才能有所帮助you@nvoigt是的,确切地说,我想把它序列化..有没有同样的方法?
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace XMLSerialization
{
[XmlRoot("CompanyEmployees")]
public class EmployeeList
{
[XmlArray("EmployeeListing")]
[XmlArrayItem("Employee", typeof(Employee))]
public List employeeList;
// Constructor
public EmployeeList()
{
employeeList = new List();
}
public void AddEmployee(Employee employee)
{
employeeList.Add(employee);
}
}
}
private void SerializeList()
{
// Create an instance of the EmployeeList class
EmployeeList employeeList = new EmployeeList();
// Create a few instances of the Employee class
Employee emp1 = new Employee();
emp1.Name = "John";
emp1.Surname = "Smith";
emp1.DateOfBirth = new DateTime(1980, 10, 08);
emp1.Sex = Employee.EmployeeSex.Male;
emp1.Position = "Software Engineer";
Employee emp2 = new Employee();
emp2.Name = "David";
emp2.Surname = "McGregor";
emp2.DateOfBirth = new DateTime(1973, 01, 13);
emp2.Sex = Employee.EmployeeSex.Male;
emp2.Position = "Product Manager";
Employee emp3 = new Employee();
emp3.Name = "Sarah";
emp3.Surname = "Crow";
emp3.DateOfBirth = new DateTime(1983, 11, 23);
emp3.Sex = Employee.EmployeeSex.Female;
emp3.Position = "Software Tester";
// Add the employees to the list
employeeList.AddEmployee(emp1);
employeeList.AddEmployee(emp2);
employeeList.AddEmployee(emp3);
// Create an instance of System.Xml.Serialization.XmlSerializer
XmlSerializer serializer = new XmlSerializer(employeeList.GetType());
// Create an instance of System.IO.TextWriter
// to save the serialized object to disk
TextWriter textWriter = new StreamWriter("C:\\Employee\\employeeList.xml");
// Serialize the employeeList object
serializer.Serialize(textWriter, employeeList);
// Close the TextWriter
textWriter.Close();
}