0

So i feel like a noob but this has been baffling me or hours now and although i have read many questions / tutorials i can't seem to get this work. No doubt this is a fundamental problem im running into that is so obvious i'm missing it out completely. Anyway, so the issue i have is this. i have a class which serialises just the way i want it, like so;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.Text;

namespace SMCProcessMonitor
{

public class Config
{
    private string recipient = "";
    private int serverport;
    private string username = "";
    private string password = "";

    public Config()
    {
    }

    public string Recipient
    {
        get
        {
            return recipient;
        }
        set
        {
            recipient= value;
        }
    }

Followed by gets / sets for the different variables. This works fine but it's the next bit i'm struggling with. Because these deal with just single instances of the code, this was relatively easy, but the next part I wish to add, is a list of programs, presumably looking something like

<program>
  <programname>program1</programname>
  <programfilepath>C:/...</programfilepath>
</program>
<program>
<programname>program2</programname>
 <programfilepath>D:/...</programfilepath>
</program>

I'm assuming here i want to use an array to store the different programs in when serializing, of which I've tried many different ways with no avail, for example. If I create the following class to hold my array details, like so:

public class Company
{
    public programs[] Programs;
}
public class Programs
{
    public string mFileName;
    public string mFilePath;
}

So then i come to use this array to fill with data, and call it using the following:

        SMCProcessMonitor.ConfigManager.mConfigurations.programs = sFile;

The left hand side seems fine, but trying to assign it to the variable sFile (which is a string holding a file name) i get several errors (depending on what i try, for example sFile.ToArray and so on, but namely conversion errors such as:

Cannot implicitly convert type 'string' to 'SMCProcessMonitor.Config.Programs[]'

so with that long winded question asked...anyone have any pointers, or tips as to where im going wrong? like i said im sure its something stupidly basic that im failing to grasp..

Cheers in advance, Shane.

4 Answers 4

2

Arrays are a bit cumbersome to use for dynamically sized collections. The easiest way is probably to go for a List, making it look something like;

public class Company
{
    public List<Program> Programs = new List<Program>();
}
public class Program
{
    public string FileName;
    public string FilePath;
}

Adding an entry would then look something like;

Programs.Add(new Program { FileName = sFile, FilePath = sPath });

...and you can access it using...

var theFirstProgram = Programs[0];
Sign up to request clarification or add additional context in comments.

1 Comment

Assuming your string of programs is XML, you can take a look at linq to xml as a quick way to parse the XML into your program objects. weblogs.asp.net/brijmohan/archive/2008/11/22/…
0

For each entry in the array you would have to add a new Programs entry, so you would need to create a Programs, and add the strings to that (mFilename, mFilePath), then add this to the array.

You will also need some space for the array.

Comments

0

XMLArray attribute?

You could also be using XMLAttribute to allow properties to be added in your XML tags, which is kinda easier to read and can be used to keep XML files that needed to be edited easy to follow.

Example...

public class Army
{
    // Fields
    private List<UnitCategory> _unitCategory;
    private string _armyName;

    // Properties
    [XmlArray("unit-category")]
    public List<UnitCategory> UnitCategory
    {
        get { return _unitCategory; }
        set { _unitCategory = value; }
    }
}
    [XmlAttribute("name")]
    public string ArmyName
    {
        get { return _armyName; }
        set { _armyName = value; }
    }

Comments

0

The issue is that sFile is a string and you are trying to assign it to an array.

You're going to need to load the file represented by sFile first, something like:

var fileStream = File.Open(sFile, FileMode.Open);

Next, you need to deserialize the file contents into the object graph:

var xs = new XmlSerializer(typeof(Company));

// this will raise an error if the contents don't match the expected object type
// else will return the Company object.
var data = (Company)xs.Deserialize(fileStream);  

Then you'll be able to assign the deserialized object to your configuration:

SMCProcessMonitor.ConfigManager.mConfigurations.programs = data.Programs;

One note I'd like to make is that your class definition for Company and Programs won't deserialize properly based on the XML that you've provided. You'll have to use some XmlAttributes to provide the right correspondencies between the XML elements and the object graph.

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.