1

I want to serialize a class Ticket into xml. I get the error :"XmlAttribute/XmlText cannot be used to encode complex type" because of my customfield class.

This is how the xml for customfields should look like ( the attribute array is nesseray but I don't understand how to create it):

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

Class Ticket

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

Class CustomFields

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

Class CustomField

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

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

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

Serialize Method

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

3 Answers 3

5

Sometimes Linq To Xml can be very helpful

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

OUTPUT:

<ticket>
  <custom_fields type="array">
    <custom_field name="Standby Reason" id="6" />
    <value />
    <custom_field name="Close Date" id="84" />
  </custom_fields>
</ticket>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, but the thing is that my class Ticket is large an contains other elements. I use another class inheriting "ISerializer" which does the convertion. Is there a way I can add the linq part to it?
@ConradC How about using other part in Linq? root.Add(new XElement(...)); etc....
I understand the principle, but I need to add the other part within <ticket>. I tried this but no sucess: new XElement(writer), new XElement("custom_fields", new XAttribute("type", "array"),
@ConradC XElement.Parse, XElement.Load, but if possible don't mix XmlSerializer and Linq2Xml
1

Class Ticket

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

Class CustomFields

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

Class 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>

1 Comment

It does not work. I mentionned above that I need the attribute array like so <custom_fields type="array">. Not a subelement.
0

List<CustomField> can not be XML attribute.

3 Comments

What is the way around? How can I serialize my class?
Insted [XmlAttribute("array")] write [XmlElement("array")]. In XML file you should use tag "array".
Sorry, insted XmlAttribute("array") use XmlArray("array")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.