2

I have the following code which outputs an object to an XML file:

using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

namespace TrailBlazerReloaded
{
    public class Config
    {
        Config config = null;
        XmlSerializer serializer = new XmlSerializer(typeof(Config));

        public Config()
        {
            CollectionPaths = null;
            Definitions = null;
            TrailerPath = null;
        }

        public string Version { get; set; }

        public string[] CollectionPaths { get; set; }

        public string[] Definitions { get; set; }

        public string[] TrailerPath { get; set; }

        public void WriteConfig(Config configToSave)
        {
            serializer = new XmlSerializer(typeof(Config));
            TextWriter textWriter = new StreamWriter(@"config.xml");
            serializer.Serialize(textWriter, configToSave);
            textWriter.Close();
        }

        public Config ReadConfig()
        {
            if (File.Exists(@"Config.xml"))
            {
                var reader = new StreamReader("Config.xml");
                config = (Config) serializer.Deserialize(reader);
                reader.Close();
            }

            return config;
        }

        public static string GetConfigFilePath()
        {
            return Assembly.GetExecutingAssembly().Location + ".config";
        }

    }
}

It returns the following result:

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Version>0.0.1</Version>
  <CollectionPaths>
    <string>F:\Trailblazer Test Folder 1</string>
    <string>F:\Trailblazer Test Folder 2</string>
    <string>F:\Trailblazer Test Folder 3 (100 Films)</string>
  </CollectionPaths>
  <Definitions>
    <string>1080p</string>
    <string>720p</string>
    <string>480p</string>
  </Definitions>
  <TrailerPath>
    <string>C:\</string>
  </TrailerPath>
</Config>

However, I would like the output to include an attribute on each of the Definition tag items like this:

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Version>0.0.1</Version>
  <CollectionPaths>
    <string>F:\Trailblazer Test Folder 1</string>
    <string>F:\Trailblazer Test Folder 2</string>
    <string>F:\Trailblazer Test Folder 3 (100 Films)</string>
  </CollectionPaths>
  <Definitions>
    <string active="true">1080p</string>
    <string active="false">720p</string>
    <string active="true">480p</string>
  </Definitions>
  <TrailerPath>
    <string>C:\</string>
  </TrailerPath>
</Config>

Any ideas? :)

1 Answer 1

4

You will have to use a custom type instead of a string.

public class Definition
{
    [XmlAttribute("active")]
    public bool Active;
    
    [XmlText]
    public string Text;
}

then define the Definitions property like so

[XmlElement("string")]
public Definition[] Definitions { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

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.