Pomster
Pomster

Reputation: 15197

Trying to Create an array holding a Struct?

I am trying to have an Array hold a Struct with two elements, to Hold a Xml Tag name and its value.

I would like to have the array working like this:

MyArrayStruct[Count].TagName = "Bla Bla";  
MyArrayStruct[Count].TagValue = "Bla Bla Bla";

Could some please help me to get this working.


public struct TagContents
{
     String TagName;
     String TagValue;        
};

I am having problems with declaring the Array as Struct to have it working like i want, i what it to be working like the comment out code.

public void LoadXML()
{
    if (File.Exists("Data.xml"))
    {
        //Readin XML
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load("Data.xml");
        XmlNodeList dataNodes = xmlDoc.SelectNodes("//FieldData");
        //Count the nodes
        int Max = 0;
        foreach (XmlNode node in dataNodes)
        {
            Max = Max + 1;
        }

        int Count = 0;
        //TagContents MyXmlPointer = new TagContents();
        // MyXmlPointer[] ArrayNode;
        //  ArrayNode = new MyXmlPointer[Max];

        foreach (XmlNode node in dataNodes)
        {
            // ArrayNode[Count].TagName =node.SelectSingleNode("Have not found what to put here yet but will get there").Name;
            // ArrayNode[Count].TagValue =node.SelectSingleNode("Have not found what to put here yet but will get there").InnerText;                      
        }
    }
    else
    {
        MessageBox.Show("Could not find file Data.xml");
    }
}

Upvotes: 3

Views: 198

Answers (1)

Ria
Ria

Reputation: 10347

Make fields public:

public class TagContent
{
    public String TagName;
    public String TagValue;
};

and use it, I suggest use generics (Like List<>):

var tags = new List<TagContent>();

tags.Add(new TagContent{TagName = "aaa", TagValue = "vvv"});

// use it:
// get value of 'TagName' of item 5:
var tagname5 = tags[5].TagName;

Upvotes: 4

Related Questions