我对 C# 编程很陌生,这是我的第一个问题。我想读取名为 ID 的窗口属性,然后将其解析为 int。
这是我的 XML 文档:
<window ID="0">
<parentID>0</parentID>
<windowType>0</windowType>
<windowName>firstWindow</windowName>
<windowText>My first window</windowText>
<windowOptions>Option 1; Option 2;</windowOptions>
<windowActions>Action 1; Action 2;</windowActions>
<windowExit>Exit Action;</windowExit>
</window>
这是我的 C# 代码,它应该从 XML 文件中读取信息,然后将它们解析为二维数组。
string[][] windowResults;
using (XmlReader reader = XmlReader.Create("GUI.xml"))
{
int windowCount = 0;
int nodeCount = 0;
int windowID = 0;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element &&
reader.Name == "window")
{
nodeCount++;
}
}
windowResults = new string[nodeCount][];
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "window")
{
while (reader.MoveToNextAttribute())
{
int.TryParse(reader.Value, out windowID);
}
}
break;
case XmlNodeType.Text:
switch (reader.Value)
{
case "parentID":
windowResults[windowID][1] = reader.Value;
break;
case "windowType":
windowResults[windowID][2] = reader.Value;
break;
case "windowText":
windowResults[windowID][3] = reader.Value;
break;
case "windowOptions":
windowResults[windowID][4] = reader.Value;
break;
case "windowActions":
windowResults[windowID][5] = reader.Value;
break;
case "windowExit":
windowResults[windowID][6] = reader.Value;
break;
}
break;
case XmlNodeType.EndElement:
switch (reader.Name)
{
case "window":
windowCount++;
break;
}
break;
}
}
}
目前它给了我以下错误:
“int”是一种“类型”,但用作“变量”