C# 如何读取xml文件(简单)

C# 如何读取xml文件(简单),c#,xml,parsing,xml-parsing,C#,Xml,Parsing,Xml Parsing,我试图在C#visual studio中阅读xml,但不知道如何做得很好。首先,我尝试了一个if块,如下所示: if (ReadToNextDescendant("painted_blue")) { // access the coordinates inside and do some work // this works } <walls_horizontal> <coord x="1" y="2" /> <coord x="1" y=

我试图在C#visual studio中阅读xml,但不知道如何做得很好。首先,我尝试了一个if块,如下所示:

if (ReadToNextDescendant("painted_blue"))
{
  // access the coordinates inside and do some work
  // this works
}
<walls_horizontal>
    <coord x="1" y="2" />
    <coord x="1" y="3" />
    <coord x="1" y="4" />
</walls_horizontal>
它成功了,我能够从绘制的蓝色坐标中提取数据。然而,我再次尝试了水平墙,但没有成功。因此,我得出结论,我需要某种新的功能,或者可能需要一种不同的方法。但是,我不擅长XML解析。首先,我不太明白ReadToDescendant到底做了什么,为什么它第二次不起作用。有人能给我指出正确的方向吗

<painted_blue>
    (0,0)
    (1,0)
    (1,5)
    (2,2)
</painted_blue>

<walls_horizontal>
    (1,2)
    (1,3)
    (1,4)
    (2,1)
    (2,2)
</walls_horizontal>

<walls_vertical>
    (1,1)
    (1,2)
    (1,3)
    (2,0)
    (2,1)
</walls_vertical>


(0,0)
(1,0)
(1,5)
(2,2)
(1,2)
(1,3)
(1,4)
(2,1)
(2,2)
(1,1)
(1,2)
(1,3)
(2,0)
(2,1)

如果可以的话,我建议您将XML格式更改为以下格式:

if (ReadToNextDescendant("painted_blue"))
{
  // access the coordinates inside and do some work
  // this works
}
<walls_horizontal>
    <coord x="1" y="2" />
    <coord x="1" y="3" />
    <coord x="1" y="4" />
</walls_horizontal>
。。。你在哪里读过这个文件

    Stream stream = File.Open(filePath, FileMode.Open);

    var reader = XmlReader.Create(stream, XmlHelper.ReaderSettings());



    if (!reader.IsEmptyElement)
            {
                string tagName = reader.Name;

                while (reader.Read() && !(reader.NodeType == XmlNodeType.EndElement))
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "wall_horizontal":
                                var coord = new Coord(reader);
                                break;

                        }
                    }
                }
            }
在自定义坐标类中,执行以下操作:

public Coord(XmlReader reader)
    {
        if (reader == null) throw new ArgumentNullException("reader");

        if (!reader.IsEmptyElement)
        {
            string tagName = reader.Name;

            while (reader.Read() && !(reader.NodeType == XmlNodeType.EndElement && reader.Name == "coord"))
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                        case "coord":
                            var x = reader.GetAttribute("x");
                            var y = reader.GetAttribute("y");
                            break;
                    }
                }

        }
        }
    }

然后用你的x和y值做些什么。这个代码是我使用的锅炉板。代码可能需要进行一些修补以适应您的情况。我希望它为你指明了正确的方向

看起来,
readtonextgenerat
是您自己的类中的一个方法,因为您没有对任何东西调用它。如果你能提供一份工作,那会很有帮助的。但一般来说,我建议在使用XML时使用LINQ到XML。但是,XML的当前结构非常不幸——看起来这些元素中的每一个都应该有5个子元素。它对于这类事情非常有用,并且是Jon Skeet在谈到LINQ到XML时所指内容的一部分。正如Jon Skeet所说,你还应该考虑修复XML,以便这些坐标是可能包含X和Y属性的子元素。值得注意的是,并添加:XML不是有效的XML。