我想要在配置文件里这样配置:
<mysection>
<url><![CDATA[http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=152674&SiteID=1]]></url>
</mysection>
而不是:
<mysection url="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=152674&SiteID=1" />
总觉得每次都将文本转义很不爽,而且有的地方是长文本,不适合作为节的子串属性。
那么该如何才能读取url元素中的 cdata 呢?
|
2个月前 阿毅 : 问题找出来了。 ConfigurationElement 不支持 CDATA 和 Text 两种 XmlNode ,除非 override DeserializeElement 方法抑制异常的抛出。 protected internal virtual void DeserializeElement(XmlReader reader, bool serializeCollectionKey) { ... reader.MoveToElement(); try { HybridDictionary dictionary = new HybridDictionary(); if (!reader.IsEmptyElement) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { ... } else { if (reader.NodeType == XmlNodeType.EndElement) { break; } if ((reader.NodeType == XmlNodeType.CDATA) || (reader.NodeType == XmlNodeType.Text)) { throw new ConfigurationErrorsException(SR.GetString("Config_base_section_invalid_content"), reader); } } } } this.EnsureRequiredProperties(serializeCollectionKey); ValidateElement(this, null, false); } catch (ConfigurationException exception) { ... } ... this.PostDeserialize(); } 看来还是把cdata内容转义成普通的 string XmlAttribute 比较合适,不然就不用标准的 Configruation 机制。 |