Home > database >  XML Attribute on Plain String
XML Attribute on Plain String

Time:05-02

I'd like to generate some XML like the following with C# code.

<card>
 <name>Cool Card</name>
 <set rarity="common">S1</set>
</card>

I have something like this.

public class card
{
    public string name = "Cool Card";
    [XmlAttribute]
    public string rarity = "common";
    public string set = "S1";
}

public static void WriteXml()
{
    var serializer = new XmlSerializer(typeof(card));
    var myCard = new();

    using var sw = new StringWriter();
    serializer.Serialize(sw, myCard);
    XDocument doc = XDocument.Parse(sw.ToString());
    XmlWriterSettings xws = new();
    xws.OmitXmlDeclaration = true;
    xws.Indent = true;
    using var xw = XmlWriter.Create(path, xws);
    doc.Save(xw);
}

The problem is that this doesn't add the "rarity" attribute to the "set" value. Trying to add enter image description here

CodePudding user response:

The cleanest option in this case is implement IXmlSerializable:

public class card : IXmlSerializable
{
    public string name = "Cool Card";
    public string rarity = "common";
    public string set = "S1";

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        reader.ReadStartElement(nameof(card));

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            if (reader.Name == nameof(name))
            {
                this.name = reader.ReadElementContentAsString();
            }
            else if (reader.Name == nameof(set))
            {
                this.rarity = reader.GetAttribute(nameof(rarity));
                this.set = reader.ReadElementContentAsString();
            }
        }

        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteElementString(nameof(name), this.name);

        writer.WriteStartElement(nameof(set));
        writer.WriteAttributeString(nameof(rarity), this.rarity);
        writer.WriteString(this.set);
        writer.WriteEndElement();                
    }
}

If your class is big and you only need do a bit change in the XML, sometimes implement IXmlSerializable is a mess (you must save all the properties and only in one or two, do a bit change). In these cases, you can use attributes and some small helpers classes to get same results. The idea is tell to XML serializer that don't serialize some property and use another fake property to do the serialization.

For example, create a Set class with your desired XML structure:

public class XmlSet
{
    private readonly card _card;

    public XmlSet()
    {
        this._card = new card();
    }

    public XmlSet(card card)
    {
        this._card = card;
    }

    [XmlText]
    public string set
    {
        get { return this._card.set; }
        set { this._card.set = value; }
    }

    [XmlAttribute]
    public string rarity
    {
        get { return this._card.rarity; }
        set { this._card.rarity = value; }
    }
}

It's only a wrapper, with the XML sttributes that you want. You get/set the values from/to your card object.

Then, in your card class, Ignore the set property and serialize the fake property:

public class card
{
    public string name = "Cool Card";
    [XmlIgnore]
    public string rarity = "common";
    [XmlIgnore]
    public string set = "S1";

    // Added to serialization
    private XmlSet _xmlSetNode;

    [XmlElement("set")]
    public XmlSet XmlSet
    {
        get
        {
            this._xmlSetNode = this._xmlSetNode ?? new XmlSet(this);
            return this._xmlSetNode;
        }
        set { this._xmlSetNode = value; }
    }
}
  • Related