【问题标题】:.NET XML serializer and XHTML string in object property.NET XML 序列化程序和对象属性中的 XHTML 字符串
【发布时间】:2019-09-29 07:20:20
【问题描述】:

我有课

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.3081.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://test/v1")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://test/v1", IsNullable=false)]
public partial class Data
{
    ...
    public object Comment { get; set; }
    ...
}

Comment 属性 属于object 类型,因为它在xml 架构中被声明为any 类型。它被声明为 any 以允许文本和 xhtml 数据。我无法更改架构 - 它与国际标准有关。

单行内容(字符串):

<Comment>This is a single line text</Comment>

多行内容(xhtml):

<Comment>
   <div xmlns="http://www.w3.org/1999/xhtml">This is text<br />with line breaks<br />multiple times<div>
</Comment>

XmlSerializer 不允许我将 XmlElement 插入到自动生成的 Data 类的 object Comment 属性中。我还尝试为 XHtml 创建自定义 IXmlSerializer 实现,但是需要将 XSD 生成的 Comment 属性声明为该确切类型(而不是对象)。

我尝试在 Comment 属性上设置的自定义 XHtml 类型如下所示;

[XmlRoot]
public class XHtmlText : IXmlSerializable
{
    [XmlIgnore]
    public string Content { get; set; }

    public XmlSchema GetSchema() => null;

    public void ReadXml(XmlReader reader) { } // Only used for serializing to XML

    public void WriteXml(XmlWriter writer)
    {
        if (Content.IsEmpty()) return;

        writer.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
        var lines = Content.Split('\n');
        for (var i = 0; i < lines.Length; i++)
        {
            var line = lines[i];
            writer.WriteRaw(line);
            if (i < lines.Length - 1) writer.WriteRaw("<br />");
        }
        writer.WriteFullEndElement();
    }
}

来自 XmlSerializer 的异常:

InvalidOperationException:不能使用类型 Lib.Xml.XHtmlText 在这种情况下。要使用 Lib.Xml.XHtmlText 作为参数,返回类型, 或类或结构的成员、参数、返回类型或成员 必须声明为 Lib.Xml.XHtmlText 类型(它不能是对象)。 Lib.Xml.XHtmlText 类型的对象不能用于非类型化 集合,例如 ArrayLists

序列化代码:

    var data = new Lib.Xml.Data { Content = "test\ntest\ntest\n" };

    var settings = new XmlWriterSettings()
    {
        NamespaceHandling = NamespaceHandling.OmitDuplicates,
        Indent = false,
        OmitXmlDeclaration = omitDeclaration,
    };

    using (var stream = new MemoryStream())
    using (var xmlWriter = XmlWriter.Create(stream, settings))
    {
        var serializer = new XmlSerializer(data.GetType(), new[] { typeof(Lib.Xml.XHtmlText) });
        serializer.Serialize(xmlWriter, data);
        return stream.ToArray();
    }

【问题讨论】:

    标签: c# .net xml xhtml xml-serialization


    【解决方案1】:

    看来我找到了解决办法。

    我无法将注释设置为 XmlElement 的实例

    data.Comment = commentAsXhtmlXmlElement;
    

    但我可以(以某种方式)分配一个 XmlNode 数组

    data.Comment = new XmlNode[] { commentAsXhtmlXmlElement };
    

    在我反序列化数据 POCO 的入站 xml 实例时发现。

    奇怪。

    【讨论】:

      猜你喜欢
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2015-05-30
      相关资源
      最近更新 更多