C# xml字符串使用c更改头编码#
我有一个XML字符串。 如何更改标题:C# xml字符串使用c更改头编码#,c#,xml,C#,Xml,我有一个XML字符串。 如何更改标题: string xml = "<?xml version='1.0' encoding='ISO-8859-8'?>"; 但是在用户对象中,我得到的字符串编码不好。 由于Xml的编码,我需要更改编码。如果Xml存储在字符串变量中,并且只需要替换编码属性中的值,则可以执行如下替换: const string searchEncoding=“ISO-8859-8”; 常量字符串newEncoding=“UTF-8”; 字符串xml=@; int e
string xml = "<?xml version='1.0' encoding='ISO-8859-8'?>";
但是在用户对象中,我得到的字符串编码不好。
由于Xml的编码,我需要更改编码。如果Xml存储在字符串变量中,并且只需要替换
编码
属性中的值,则可以执行如下替换:
const string searchEncoding=“ISO-8859-8”;
常量字符串newEncoding=“UTF-8”;
字符串xml=@;
int encodingPos=xml.IndexOf(searchEncoding);
如果(编码位置==30)
{
xml=xml.Substring(0,encodingPos)+newEncoding+xml.Substring(encodingPos+searchEncoding.Length);
}
但是,如果XML存储在另一个数据类型中,并且/或者您需要重新编码XML内容,则需要另一个过程。而不是
编码。UTF8.GetBytes
使用编码。GetEncoding(“ISO-8859-8”)。GetBytes
不完全清楚上下文是什么。想必您也想对所有数据进行重新编码,而不仅仅是更改声明?请提供更多细节。
string xml = "<?xml version='1.0' encoding='UTF-8'?>";
XmlSerializer serializer = new XmlSerializer(typeof(User));
MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
User user = (User)serializer.Deserialize(memStream);