我正在尝试使用一个数组来保存带有两个元素的Struct,以保存Xml标记名称及其值。
我想让数组像这样工作:
MyArrayStruct[Count].TagName = "Bla Bla";
MyArrayStruct[Count].TagValue = "Bla Bla Bla";
有些人可以帮我解决这个问题。
public struct TagContents
{
String TagName;
String TagValue;
};
我在将Array声明为Struct以使其像我想要的那样工作时遇到问题,我的工作方式就像注释掉代码一样。
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");
}
}
答案 0 :(得分:4)
制作字段public
:
public class TagContent
{
public String TagName;
public String TagValue;
};
并使用它,我建议使用泛型(如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;