提问人:Learner 提问时间:4/18/2023 更新时间:4/18/2023 访问量:36
DataContarctResolver System.ArgumentException “<>c”中的名称字符无效。名称中不能包含十六进制值“<”字符(十六进制值0x3C)
DataContarctResolver System.ArgumentException Invalid name character in '<>c'. The '<' character, hexadecimal value 0x3C,cannot be included in a name
问:
我正在使用 DataContractSerializer 和 DataContractResolver 来替换 NetDataContractSerializer 的功能,因为我正在将我的框架库移植到 .Net 标准 2.0,并且由于无法解析特殊字符(如 '、<、>TypeName 和 TypeNamespace)而出现错误。对于其他标记,特殊字符将替换为其各自的实体引用。
我正在遵循这个来实现我的解析器 https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractresolver?view=netstandard-2.0your text
// Used at deserialization
// Allows users to map xsi:type name to any Type
public override Type ResolveName(string typeName, string typeNamespace, Type declaredType, DataContractResolver knownTypeResolver)
{
XmlDictionaryString tName;
XmlDictionaryString tNamespace;
if (dictionary.TryGetValue(typeName, out tName) && dictionary.TryGetValue(typeNamespace, out tNamespace))
{
return this.assembly.GetType(tNamespace.Value + "." + tName.Value);
}
else
{
return null;
}
}
// Used at serialization
// Maps any Type to a new xsi:type representation
public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
{
string name = type.Name;
string namesp = type.Namespace;
typeName = new XmlDictionaryString(XmlDictionary.Empty, name, 0);
typeNamespace = new XmlDictionaryString(XmlDictionary.Empty, namesp, 0);
if (!dictionary.ContainsKey(type.Name))
{
dictionary.Add(name, typeName);
}
if (!dictionary.ContainsKey(type.Namespace))
{
dictionary.Add(namesp, typeNamespace);
}
return true;
}
我不明白为什么 TypeName 中的特殊字符会替换为它们各自的实体引用。任何帮助将不胜感激。
答: 暂无答案
评论