如何使用XmlReader将属性值作为int获取?

时间:2022-11-27 23:39:51

I'm very new to C# programming, here's my first question. I'd like to read the attribute named ID of window and then parse it into an int.

我是C#编程的新手,这是我的第一个问题。我想读取名为ID of window的属性,然后将其解析为int。

Here's my XML document:

这是我的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>

Here's my C# code, it should read the information from the XML files and then parse them into a 2D array.

这是我的C#代码,它应该从XML文件中读取信息,然后将它们解析为2D数组。

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;
        }
    }
}

Currently it gives me the following error:

目前它给我以下错误:

'int' is a 'type' but is used like a 'variable'

'int'是'type'但是像'变量'一样使用

1 个解决方案

#1


1  

The same using LINQ to XML:

使用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"),
           };
}

A sequence of the following classes will be constructed:

将构造以下类的序列:

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; }
}

#1


1  

The same using LINQ to XML:

使用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"),
           };
}

A sequence of the following classes will be constructed:

将构造以下类的序列:

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; }
}