StructToString / StringToStruct
2008-07-15 10:36
190 查看
//使用 XML 序列化将结构或对象转换成字符串
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
[Serializable]
public struct MyStruct
{
public int i;
}
public class Program
{
static void Main(string[] args)
{
MyStruct myStruct = new MyStruct();
myStruct.i = 123;
XmlSerializer serializer = new XmlSerializer(typeof(MyStruct));
// Struct To String
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, myStruct);
stream.Seek(0, SeekOrigin.Begin);
string s = Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine(s);
// String to Struct
MemoryStream stream2 = new MemoryStream(Encoding.UTF8.GetBytes(s));
MyStruct myStruct2 = (MyStruct)serializer.Deserialize(stream2);
Console.WriteLine(myStruct2.i);
}
}
}
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
[Serializable]
public struct MyStruct
{
public int i;
}
public class Program
{
static void Main(string[] args)
{
MyStruct myStruct = new MyStruct();
myStruct.i = 123;
XmlSerializer serializer = new XmlSerializer(typeof(MyStruct));
// Struct To String
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, myStruct);
stream.Seek(0, SeekOrigin.Begin);
string s = Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine(s);
// String to Struct
MemoryStream stream2 = new MemoryStream(Encoding.UTF8.GetBytes(s));
MyStruct myStruct2 = (MyStruct)serializer.Deserialize(stream2);
Console.WriteLine(myStruct2.i);
}
}
}
相关文章推荐
- Convert IP addresses from a dots-and-number string to a struct in_addr and back
- SVN svn: Can't convert string from 'UTF-8' to native encoding:
- [leetcoide] 【字符串】8. String to Integer (atoi)
- JavaScript toLocaleString()
- decimal toString format (C#)
- js中通过Object.prototype.toString方法----精判断对象的类型
- Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec
- Application.Count.ToString()和Application["count"].ToString()的区别
- string.tostring 格式
- String to Integer (atoi)
- 编译安装PHP,解决问题 Don't know how to define struct flock on this system, set --enable-opcache=no
- StringtoDate.htm
- 进制转换及stringToInt
- The method decodeBuffer(String) from the type characterDecoder is not accessible due to ...
- JavaScript中的toString()和toLocaleString()方法的区别
- jsp页面出现“String cannot be resolved to a type”异常解决方法
- Words and Text: Using String Variables to Organize Words
- leetCode 之 String to Interger
- 编写高质量代码改善java程序的151个建议——[52-57]String !about String How to use them?