【发布时间】:2021-11-20 13:26:15
【问题描述】:
当我反序列化一个 xml 字符串时,我需要在一个名为 prop2 的字符串属性上保存一个 XElement outerXml。
我的 XML:
<MyObj>
<prop1>something</prop1>
<prop2>
<RSAKeyValue>
<Modulus>...</Modulus>
<Exponent>...</Exponent>
</RSAKeyValue>
</prop2>
<prop3></prop3>
</MyObj>
我的对象:
public class MyObj
{
[XmlElement("prop1")]
public string prop1 { get; set; }
[XmlText]
public string prop2 { get; set; }
[XmlElement(ElementName = "prop3", IsNullable = true)]
public string prop3 { get; set; }
}
我正在使用XmlSerializer 进行反序列化,如下所示:
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(new StringReader(myXmlString));
我尝试使用[XmlText] 将XML 文本保存在prop2 中,但我只得到null。
我需要做什么才能将<RSAKeyValue><Modulus>...</Modulus><Exponent>...</Exponent></RSAKeyValue> 中的文本保存在prop2 中?
【问题讨论】:
-
@MethodMan 我看不到哪里有重复...在那个问题中,他们需要将每个 xml 元素反序列化为一个属性,但我需要将所有 RSAKeyValue 外部 xml 反序列化为单个属性作为文本字符串
-
@MethodMan 相信我,我从今天早上开始就已经这样做了。但我的问题是一个特殊的问题。
-
你不能只有自定义类来读取这个属性(这样做会更容易,如果你仍然需要字符串 - 创建额外的非序列化属性来读取它)
-
@AlexeiLevenkov 我可以,但
[XmlText]不应该这样做吗?因为那时我需要使用prop2作为字符串,总是。
标签: c# xml serialization xml-serialization xmlserializer