如何序列化的字节数组在C#中使用XML XmlSerializer的? [英] How to serialize byte array to XML using XmlSerializer in C#?
问题描述
假设我们有一个结构,它的数据是用Marshal.PtrToStructure联合国管理的字节数组提供
Say we have a struct that it's data is provided by un-managed byte array using Marshal.PtrToStructure.
C#的结构布局:
[StructLayout(LayoutKind.Sequential, Size = 128, CharSet = CharSet.Ansi, Pack = 1)]
public struct MNG_Y_Params
{
public byte Number;
public byte Version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] OliNumber;
public byte InterfaceType;
}
字节数组代表了非托管代码(ASCII)字符串。
The byte array represent a (ascii) string in the un-managed code.
这结构是另一种结构的成员(即有一定的其他成员):
This struct is a member of another struct (that has some other members):
public struct MyData
{
public int ID;
public StructType structType;
[XmlElement(ElementName="MNG_Y_Params")]
public MNG_Y_Params y_params;
[XmlElement(ElementName = "SimpleStruct2")]
public SimpleStruct2 ss2;
};
所以,我们也有这方面的支持代码
So we also have this support code
public class XMLIgnore
{
static public XmlSerializer customserialiser(MyData d)
{
XmlAttributes attrs = new XmlAttributes();
attrs.XmlIgnore = true;
XmlAttributeOverrides xmlOveride = new XmlAttributeOverrides();
switch (d.structType)
{
case StructType.ST_1:
xmlOveride.Add(typeof(MyData), "ss2", attrs);
break;
case StructType.ST_2:
xmlOveride.Add(typeof(MyData), "y_params", attrs);
break;
default:
break;
}
return new XmlSerializer(typeof(MyData), xmlOveride);
}
}
和保存方法
static void SaveToXml(object obj, string fileName, XmlSerializer writer)
{
//XmlSerializer writer = new XmlSerializer(obj.GetType());
using (StreamWriter file = new StreamWriter(fileName))
{
writer.Serialize(file, obj);
}
}
有关的例子中,我们只希望产生一些数据。
For the example we'd just generate some data.
MNG_Y_Params yParams = new MNG_Y_Params();
yParams.Version = 1;
yParams.InterfaceType = 15;
yParams.Number = 35;
ASCIIEncoding enc = new ASCIIEncoding();
yParams.OliNumber = enc.GetBytes("#1");
MyData md1 = new MyData();
md1.ID = 1;
md1.structType = StructType.ST_1;
md1.y_params = yParams;
XmlSerializer writer = XMLIgnore.customserialiser(md1);
SaveToXml(md1, @"C:\temp\dataOne.xml", writer);
预计XML:
Expected XML:
<?xml version="1.0" encoding="utf-8"?>
<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>1</ID>
<structType>ST_1</structType>
<MNG_Y_Params>
<Number>35</Number>
<Version>1</Version>
<OliNumber>#1</OliNumber>
<InterfaceType>15</InterfaceType>
</MNG_Y_Params>
</MyData>
结果XML:
Result XML:
<?xml version="1.0" encoding="utf-8"?>
<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>1</ID>
<structType>ST_1</structType>
<MNG_Y_Params>
<Number>35</Number>
<Version>1</Version>
<OliNumber>IzE=</OliNumber>
<InterfaceType>15</InterfaceType>
</MNG_Y_Params>
</MyData>
请关注字节数组成员(OliNumber)结果上。
Please focus on the byte array member (OliNumber) result.
有,我们可以在这里使用一个属性?
我缺少什么?
Is there an attribute that we could use here? What am I missing?
感谢您的时间和帮助。
宜兰
有关的引用
推荐答案
的的XmlSerializer
在默认情况下将使用base 64编码编码的字节数组。如果您使用此网站,并在 IZE粘贴=
,并对其进行解码,其结果将是 1
。您可以通过设置的 XmlElementAttribute.DataType 。我不知道,如果 [XmlElement的(数据类型=字符串)]
将工作,但你可以试试。使用 [XmlElement的(数据类型=hexBinary的)]
将产生的原始字节。
The XmlSerializer
by default will encode the byte arrays using base 64 encoding. If you use this site and paste in IzE=
, and decode it, the result will be #1
. You can change the encoding by setting the XmlElementAttribute.DataType. I'm not sure if [XmlElement(DataType = "string")]
will work, but you can try it. Using [XmlElement(DataType = "hexBinary")]
will generate the raw bytes.
这篇关于如何序列化的字节数组在C#中使用XML XmlSerializer的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!