Chetan Hirapara
Chetan Hirapara

Reputation: 694

How to create xml with attribute using XmlSerializer?

I'm working on xml generation using list to direct xml of generic class, but getting wrong result of xml.

I want below output from XmlSerializer.

 <root>
    <rating city="A" code="A1">A++</rating>
    <rating city="B" code="A2">A</rating>
    <rating city="C" code="A3">AB</rating>
</root>

currently i'm getting this

<ratings reason="reason123">
<rating>R</rating>
</ratings>
<ratings reason="reason123">
<rating>R</rating></ratings>

C# Code

public class root
{
    [XmlAttribute("city")]
    public string city { get; set; }
    [XmlAttribute("code")]
    public string code { get; set; }
    [XmlElement("rating")]
    public string rating { get; set; }
}
string tempXML6 = XmlExtensions.Serialize(rootList)

static public void Serialize(object classobject)
{
     XmlSerializer SerializedObject = new XmlSerializer(classobject.GetType());
            using (MemoryStream xmlStream = new MemoryStream())
            {
                SerializedObject.Serialize(xmlStream, classobject);
                xmlStream.Position = 0;
                //Loads the XML document from the specified string.
                XmlDocument resultDocument = new XmlDocument();
                resultDocument.Load(xmlStream);
                if (!string.IsNullOrEmpty(resultDocument.DocumentElement.InnerXml))
                {
                    return resultDocument.DocumentElement.InnerXml;
                }
                else 
                {
                    return string.Empty;
                }
            }
}

please let me know what's needs to be change.

Upvotes: 1

Views: 87

Answers (3)

Ponas Justas
Ponas Justas

Reputation: 305

Your object model is wrong. It should be something like this.

[XmlRoot("root")]
public class Root
{
    [XmlElement("rating")]
    public Rating[] Ratings { get; set; }
}

public class Rating
{
    [XmlAttribute("city")]
    public string City { get; set; }
    [XmlAttribute("code")]
    public string Code { get; set; }
    [XmlText]
    public string Value { get; set; }
}

To serialize the object do this

Root root = new Root
{
    Ratings = new Rating[]
    {
        new Rating { City = "A", Code = "A1", Value = "A++" },
        new Rating { City = "B", Code = "A2", Value = "A" },
        new Rating { City = "C", Code = "A3", Value = "AB" }
    }
};

string serializedObject = Serialize(root);

This is the serialize method

public static string Serialize<T>(T item)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    using (StringWriter stringWriter = new StringWriter())
    {
        using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))
        {
            serializer.Serialize(xmlWriter, item, new XmlSerializerNamespaces(new[] { new XmlQualifiedName(string.Empty, string.Empty) }));
        }

        return stringWriter.ToString();
    }
}

Xml output

<root>
    <rating city="A" code="A1">A++</rating>
    <rating city="B" code="A2">A</rating>
    <rating city="C" code="A3">AB</rating>
</root>

Upvotes: 1

Amit Pore
Amit Pore

Reputation: 127

    //try this 
    public static XElement SerializeRoot(IEnumerable<root> objRoot)
    {

        XElement serializedRoot = null;

        using (MemoryStream memoryStream = new MemoryStream())
        {

            using (TextWriter StreamWriter = new StreamWriter(memoryStream))
            {

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<root>));

                xmlSerializer.Serialize(StreamWriter, objRoot);

                serializedRoot = XElement.Parse(Encoding.UTF8.GetString(memoryStream.ToArray()));

            }

        }

        return serializedRoot;

    }

Upvotes: 0

Saadi
Saadi

Reputation: 2237

Please Change your Class defination to like this:

[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class root
{
    private rootRating[] ratingField;

    [XmlElementAttribute("rating")]
    public rootRating[] rating
    {
        get
        {
            return this.ratingField;
        }
        set
        {
            this.ratingField = value;
        }
    }
}

[XmlTypeAttribute(AnonymousType = true)]
public partial class rootRating
{

    private string cityField;

    private string codeField;

    private string valueField;

    [XmlAttributeAttribute()]
    public string city
    {
        get
        {
            return this.cityField;
        }
        set
        {
            this.cityField = value;
        }
    }

    [XmlAttributeAttribute()]
    public string code
    {
        get
        {
            return this.codeField;
        }
        set
        {
            this.codeField = value;
        }
    }

    [XmlTextAttribute()]
    public string Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}

Upvotes: 0

Related Questions