【问题标题】:XmlAttribute/XmlText cannot be used to encode complex typeXmlAttribute/XmlText 不能用于编码复杂类型
【发布时间】:2012-11-16 16:35:58
【问题描述】:

我想将一个类Ticket 序列化为xml。由于我的自定义字段类,我收到错误消息:“XmlAttribute/XmlText 不能用于编码复杂类型”。

自定义字段的 xml 应该是这样的(属性数组是 nesseray,但我不明白如何创建它):

<custom_fields type="array">
<custom_field name="Standby Reason" id="6">
<value/>
</custom_field>
<custom_field name="Close Date" id="84">

课票

public class Ticket
{
    [XmlElement("custom_fields")]
    public CustomFields Custom_fields { get; set; }

类自定义字段

[Serializable]
public class CustomFields
{
    [XmlAttribute("array")]
    public List<CustomField> custom_field { get; set; }

类自定义字段

[Serializable]
public class CustomField
{
    [XmlIgnore]
    public string Name { get; set; }

    [XmlElement]
    public int Id { get; set; }

    [XmlElement]
    public string Value { get; set; }

序列化方法

public string Serialize(object obj)
{
    var nsSerializer = new XmlSerializerNamespaces();
    nsSerializer.Add(String.Empty, String.Empty);

    var serializer = new XmlSerializer(typeof(Ticket), String.Empty);

    using (StringWriter writer = new StringWriter())
    {
        ExtendedXmlTextWriter xmlTextWriter = new ExtendedXmlTextWriter(writer);
        serializer.Serialize(xmlTextWriter, obj, nsSerializer);

        //return writer.ToString();

        XElement root = new XElement("custom_fields", new XAttribute("type", "array"),
            new XElement("custom_field",
                new XAttribute("name", "Standby Reason"),
                new XAttribute("id", 6)
                ), new XElement("value"),
                    new XElement("custom_field",
                        new XAttribute("name", "Close Date"),
                        new XAttribute("id", 84)
                        )
                        );

        return (writer.ToString() + root.ToString());
    }

【问题讨论】:

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


    【解决方案1】:

    Linq To Xml 有时会很有帮助

    XElement root = new XElement("ticket",
                            new XElement("custom_fields",
                                new XAttribute("type", "array"),
                                new XElement("custom_field",
                                    new XAttribute("name", "Standby Reason"),
                                    new XAttribute("id", 6)
                                ),
                                new XElement("value"),
                                new XElement("custom_field",
                                    new XAttribute("name", "Close Date"),
                                    new XAttribute("id", 84)
                                )
                            )
                    );
    
    string xml = root.ToString();
    

    输出:

    <ticket>
      <custom_fields type="array">
        <custom_field name="Standby Reason" id="6" />
        <value />
        <custom_field name="Close Date" id="84" />
      </custom_fields>
    </ticket>
    

    【讨论】:

    • 谢谢,但问题是我的班级 Ticket 很大并且包含其他元素。我使用另一个继承“ISerializer”的类来进行转换。有没有办法可以将 linq 部分添加到它?
    • @ConradC 在 Linq 中使用其他部分怎么样? root.Add(new XElement(...)); 等等......
    • 原理明白了,但是需要在里面加上其他部分。我试过了,但没有成功: new XElement(writer), new XElement("custom_fields", new XAttribute("type", "array"),
    • @ConradC XElement.Parse, XElement.Load,但如果可能,不要混合使用 XmlSerializer 和 Linq2Xml
    【解决方案2】:

    班票

    public class Ticket
    {
        [XmlElement("custom_fields")]
        public CustomFields Custom_fields { get; set; }
    

    类自定义字段

    [Serializable]
    public class CustomFields
    {
        [XmlArray("array"), XmlArrayItem("custom_field")]
        public List<CustomField> custom_field { get; set; }
    

    类自定义字段

    [Serializable]
    public class CustomField
    {
        [XmlAttribute("name")]
        public string Name { get; set; }
    
        [XmlAttribute("id")]
        public int Id { get; set; }
    
        [XmlElement("value")]
        public string Value { get; set; }
    

    XML:

    <ticket>
       <custom_fields>
         <array>
            <custom_field name="Standby Reason" id="6"><value /></custom_field>
            <custom_field name="Close Date" id="84"><value /></custom_field>
         </array>
       </custom_fields>
    </ticket>
    

    【讨论】:

    • 它不起作用。我在上面提到我需要像 这样的属性数组。不是子元素。
    【解决方案3】:

    List 不能是 XML 属性。

    【讨论】:

    • 有什么办法吗?如何序列化我的课程?
    • Insted [XmlAttribute("array")] 写入 [XmlElement("array")]。在 XML 文件中,您应该使用标签“数组”。
    • 对不起,插入 XmlAttribute("array") 使用 XmlArray("array")
    猜你喜欢
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多