c# - XmlAttribute/XmlText 不能用于编码复杂类型

标签 c# .net xml serialization xml-serialization

我想将类 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">

类(class)票

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());
    }

最佳答案

有时 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>

关于c# - XmlAttribute/XmlText 不能用于编码复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13420670/

相关文章:

c# - 字典中不存在给定的键。哪把 key ?

c# - 为什么 ComboBox 在设置 DataSource 时不抛出异常?

c# - XmlDocument.SelectNodes 问题

c# - 更新 net5.0 .csproj 到 net6.0

xml - 需要属性但也允许 nil 的 XSD

c# - 如何从 ASP.NET Web Api 中的绑定(bind)中排除某些属性

c# - 指定的容器不存在

c# - 如何通过 Web API 跟踪断开连接的实体

c# - 在 C# 中使用 XML 的 jQuery 语法

android - backgroundTint 对 Lollipop 版本没有影响