如何使用XmlSerializer插入xml字符串 [英] How do I use XmlSerializer to insert an xml string
本文介绍了如何使用XmlSerializer插入xml字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我定义了以下类别:
public class Root
{
public string Name;
public string XmlString;
}
并创建了一个对象:
Root t = new Root
{ Name = "Test",
XmlString = "<Foo>bar</Foo>"
};
当我使用XmlSerializer类序列化此对象时,它将返回xml:
When I use XmlSerializer class to serialize this object, it will return the xml:
<Root>
<Name>Test</Name>
<XmlString><Foo>bar</Foo></XmlString>
</Root>
如何使其不对XmlString内容进行编码,以便我可以将序列化的xml作为
How do I make it not encode my XmlString content so that I can get the serialized xml as
<XmlString><Foo>bar</Foo></XmlString>
谢谢,伊恩
推荐答案
您可以将自定义序列化限制为仅需要特别注意的元素,像这样.
You can limit the custom serialization to just the element that needs special attention like so.
public class Root
{
public string Name;
[XmlIgnore]
public string XmlString
{
get
{
if (SerializedXmlString == null)
return "";
return SerializedXmlString.Value;
}
set
{
if (SerializedXmlString == null)
SerializedXmlString = new RawString();
SerializedXmlString.Value = value;
}
}
[XmlElement("XmlString")]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public RawString SerializedXmlString;
}
public class RawString : IXmlSerializable
{
public string Value { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
this.Value = reader.ReadInnerXml();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteRaw(this.Value);
}
}
这篇关于如何使用XmlSerializer插入xml字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文