如何防止XmlSerialzer转义“嵌套XML"? [英] How to prevent XmlSerialzer from escaping "nested XML"?
问题描述
我正在使用XmlSerializer序列化/反序列化复杂对象.一个属性包含一个XML字符串,应将其写入字符串属性中,而无需反序列化.
I am using an XmlSerializer to serialize / deserialize complex objects. One property contains an XML string that should be written into a string property without deserialization.
示例(可在LinqPad中执行):
[XmlRoot("RootObject")]
[Serializable]
public class RootClass
{
[XmlArray("SubObjects")]
[XmlArrayItem("SubObject")]
public SubClass[] SubObjecs { get; set;}
}
[Serializable]
public class SubClass
{
[XmlElement("XmlConfiguration")]
public string XmlConfiguration { get; set;}
}
void Main()
{
var obj = new RootClass()
{
SubObjecs = new[]
{
new SubClass { XmlConfiguration = "<ConfigurationX>SomeConfiguration1</ConfigurationX>" },
new SubClass { XmlConfiguration = "<ConfigurationY>SomeConfiguration2</ConfigurationY>" }
}
};
var serializer = new XmlSerializer(typeof(RootClass));
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, obj);
stream.Position = 0;
Console.WriteLine(Encoding.UTF8.GetString(stream.GetBuffer()));
}
}
该示例的输出为:
<?xml version="1.0"?>
<RootObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SubObjects>
<SubObject>
<XmlConfiguration><ConfigurationX>SomeConfiguration1</ConfigurationX></XmlConfiguration>
</SubObject>
<SubObject>
<XmlConfiguration><ConfigurationY>SomeConfiguration2</ConfigurationY></XmlConfiguration>
</SubObject>
</SubObjects>
</RootObject>
XML是一个配置文件,有时以编程方式编写,但主要由人编写/修改.因此, XmlConfiguration
中的XML不应包含转义字符.
The XML is a configuration file that is sometimes written programmatically, but mainly written / modified by humans. Therefore the XML within XmlConfiguration
should not contain escaped characters.
问题:是否可以防止XmlSerializer转义<"和'>'字符?如果不是,是否可以使用另一个串行器?
Question: Is it possible to prevent the XmlSerializer from escaping the '<' and '>' characters? If not, Is there another serializer that can be used?
一个有效的选项是 XmlWriter.WriteRaw.但是,如果可以的话,我会避免这种不可靠且难以维护的解决方案.
One option that works is XmlWriter.WriteRaw. However, if somehow possible I would avoid that unreliable and less maintainable solution.
我在这里发现了类似的问题:如何防止XmlSerializer逃逸<和>字符.但是该问题与!CDATA [[Content]]有关,无法解决我的问题.
I found a similar question here: How to prevent XmlSerializer from escaping < and > characters. But that question is related to !CDATA[[Content]] and has no answer for my problem.
推荐答案
如dbc在上面的注释中所述,有一种使用 XmlAnyElement
属性的解决方案,如下所述:
As mentioned above in comment by dbc, there is a solution that uses the XmlAnyElement
attribute as described here: Deserialize dynamic XML
我找到了混合了 XmlSerializer
和 XmlWriter.WriteRaw
的解决方案.在实现 IXmlSerializable
时,可以控制 XmlSerializer
的序列化过程.只能为需要特殊处理的类实现IXmlSerializable(对于我来说这是可以的):
I found a solution that is a mixture of XmlSerializer
and XmlWriter.WriteRaw
. When implementing IXmlSerializable
, it is possible to control the serialization process of the XmlSerializer
. Therfore IXmlSerializable has to be implemented just for the class that needs special handling (which is OK for me):
[Serializable]
public class SubClass : IXmlSerializable
{
[XmlElement("XmlConfiguration")]
public string XmlConfiguration { get; set; }
public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("XmlConfiguration");
writer.WriteRaw(XmlConfiguration);
writer.WriteEndElement();
}
public void ReadXml(XmlReader reader)
{
reader.ReadToDescendant("XmlConfiguration");
XmlConfiguration = reader.ReadInnerXml();
reader.ReadEndElement();
}
public XmlSchema GetSchema()
{
return (null);
}
}
这篇关于如何防止XmlSerialzer转义“嵌套XML"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!