1

我想将一个类序列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; }

类 CustomField

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

3 回答 3

5

有时 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>
于 2012-11-16T17:07:18.527 回答
1

班票

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

类 CustomField

[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>
于 2012-11-16T16:57:18.713 回答
0

List<CustomField> 不能是 XML 属性。

于 2012-11-16T16:39:04.233 回答