gpt4 book ai didi

c# - 使用特殊字符反序列化 XML 的快速方法

转载 作者:数据小太阳 更新时间:2023-10-29 01:54:40 25 4
gpt4 key购买 nike

我正在寻找反序列化 xml 的快速方法,其中包含特殊字符,如 ö。

我使用的是 XMLReader,它无法反序列化这些字符。

有什么建议吗?

编辑:我正在使用 C#。代码如下:

XElement element =.. //has the xml
XmlSerializer serializer = new XmlSerializer(typeof(MyType));
XmlReader reader = element.CreateReader();
Object o= serializer.Deserialize(reader);

最佳答案

我猜你遇到了编码问题,而不是在 XMLReader 中但是有了 XmlSerializer .

你可以使用XmlTextWriter和 UTF8 编码与 XmlSerializer就像在下面的代码片段中一样(请参阅下面的通用方法以获得更好的实现方式)。与元音变音符 (äöü) 和其他特殊字符 配合得很好。

class Program
{
static void Main(string[] args)
{
SpecialCharacters specialCharacters = new SpecialCharacters { Umlaute = "äüö" };

// serialize object to xml

MemoryStream memoryStreamSerialize = new MemoryStream();
XmlSerializer xmlSerializerSerialize = new XmlSerializer(typeof(SpecialCharacters));
XmlTextWriter xmlTextWriterSerialize = new XmlTextWriter(memoryStreamSerialize, Encoding.UTF8);

xmlSerializerSerialize.Serialize(xmlTextWriterSerialize, specialCharacters);
memoryStreamSerialize = (MemoryStream)xmlTextWriterSerialize.BaseStream;

// converts a byte array of unicode values (UTF-8 enabled) to a string
UTF8Encoding encodingSerialize = new UTF8Encoding();
string serializedXml = encodingSerialize.GetString(memoryStreamSerialize.ToArray());

xmlTextWriterSerialize.Close();
memoryStreamSerialize.Close();
memoryStreamSerialize.Dispose();

// deserialize xml to object

// converts a string to a UTF-8 byte array.
UTF8Encoding encodingDeserialize = new UTF8Encoding();
byte[] byteArray = encodingDeserialize.GetBytes(serializedXml);

using (MemoryStream memoryStreamDeserialize = new MemoryStream(byteArray))
{
XmlSerializer xmlSerializerDeserialize = new XmlSerializer(typeof(SpecialCharacters));
XmlTextWriter xmlTextWriterDeserialize = new XmlTextWriter(memoryStreamDeserialize, Encoding.UTF8);

SpecialCharacters deserializedObject = (SpecialCharacters)xmlSerializerDeserialize.Deserialize(xmlTextWriterDeserialize.BaseStream);
}
}
}

[Serializable]
public class SpecialCharacters
{
public string Umlaute { get; set; }
}

我个人使用以下通用方法来序列化和反序列化 XML 和对象,并且还没有遇到任何性能或编码问题。

public static string SerializeObjectToXml<T>(T obj)
{
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

xmlSerializer.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

string xmlString = ByteArrayToStringUtf8(memoryStream.ToArray());

xmlTextWriter.Close();
memoryStream.Close();
memoryStream.Dispose();

return xmlString;
}

public static T DeserializeXmlToObject<T>(string xml)
{
using (MemoryStream memoryStream = new MemoryStream(StringToByteArrayUtf8(xml)))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

using (StreamReader xmlStreamReader = new StreamReader(memoryStream, Encoding.UTF8))
{
return (T)xmlSerializer.Deserialize(xmlStreamReader);
}
}
}

public static string ByteArrayToStringUtf8(byte[] value)
{
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetString(value);
}

public static byte[] StringToByteArrayUtf8(string value)
{
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(value);
}

关于c# - 使用特殊字符反序列化 XML 的快速方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4899872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com