2012-05-04 27 views
0

我对C#编程非常陌生,这是我的第一个问题。 我想读取名为ID的窗口属性,然后解析成int。如何使用XmlReader以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' is a 'type' but is used like a 'variable'

+2

您发布的代码不会生产e编译器错误。请确认错误的来源。 – vcsjones

+0

它指向包含while的行(reader.MoveToNextAttribute()) – DaMaGeX

回答

1

的使用LINQ to XML一样:

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

private static IEnumerable<Window> ReadWindows(string path) 
{ 
    XDocument doc = XDocument.Load(path); 
    return from w in doc.Root.Elements("window") 
      select new Window((int)w.Attribute("ID")) 
      { 
       ParentID = (int?)w.Element("parentID"), 
       Type = (string)w.Element("windowType"), 
       Name = (string)w.Element("windowName"), 
       Text = (string)w.Element("windowText"), 
       Options = (string)w.Element("windowOptions"), 
       Actions = (string)w.Element("windowActions"), 
       Exit = (string)w.Element("windowExit"), 
      }; 
} 

以下类别的顺序将兴建:

class Window 
{ 
    public Window(int id) { this.ID = id; } 

    public int ID { get; private set; } 
    public int? ParentID { get; set; } 
    public string Type { get; set; } 
    public string Name { get; set; } 
    public string Text { get; set; } 
    public string Options { get; set; } 
    public string Actions { get; set; } 
    public string Exit { get; set; } 
} 
+0

感谢您的回答。这确实看起来更容易,然后我的代码。尽管我有几个问题。我想列出来自xml文件下的窗口。如何在标签或链接上打印窗口文本,然后检查窗口是否有任何孩子。列在下面。然后进入下一个无父窗口? – DaMaGeX

+0

另外我在哪里放第一个片段?把它放在'private void Form1_Load(object sender,EventArgs e)'给了我下面的编译器错误。 '因为'WindowsFormsApplication1.mainForm.Form1_Load(object,System.EventArgs)'返回void,所以返回关键字后面不能有对象表达式' – DaMaGeX

+0

@DaMaGeX:我已经更新了我的答案以涵盖您的一些问题。如何创建用户界面并将结果绑定到该用户界面上 - 请发布另一个问题的主题。 – abatishchev