如何正确循环 XmlTextReader (C#)?

How to loop XmlTextReader properly (C#)?

下面是我尝试处理的 XML 文件类型的示例。如果我只有一个部分以及随附的 number/character,我可以在不需要 'if (!reader.EOF)' 控制结构的情况下处理数据提取。但是,当我尝试包含此结构以便我可以返回检查另一个部分、数字和字符组时,它会死锁。

关于如何正确执行此操作的任何建议?这是我脑海中闪现的最有效的想法。我是从 XMLs.

读取数据的新手

样本Xml:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <part>100B</part>
  <number>45</number>
  <character>a</character>

  <part>100C</part>
  <number>55</number>
  <character>b</character>
</note>

代码:

String part = "part";
String number = "number";
String character = "character";
String appendString = "";
StringBuilder sb = new StringBuilder();

try
{
    XmlTextReader reader = new XmlTextReader("myPath");
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element: // The node is an element.

                myLabel:
                if (reader.Name == part)
                {
                    part = reader.ReadInnerXml();
                }
                if (reader.Name == number)
                {
                    number = reader.ReadInnerXml();
                    number = double.Parse(number).ToString("F2"); //format num
                }
                if (reader.Name == character)
                {
                    character = reader.ReadInnerXml();
                }

                //new string
                appendString = ("Part: " + part + "\nNumber: " + number +
 "\nCharacter: " + character + "\n");

                //concatenate
                sb.AppendLine(appendString);

                if (reader.EOF != true)
                {
                    Debug.Log("!eof");
                    part = "part";
                    number = "number";
                    character = "character";
                    goto myLabel;
                }

                //print fully concatenated result
                sb.ToString();

                //reset string builder
                sb.Length = 0;

                break;
        }
    }
}
catch (XmlException e)
{
    // Write error.
    Debug.Log(e.Message);
}
catch (FileNotFoundException e)
{
    // Write error.
    Debug.Log(e);
}
catch(ArgumentException e)
{
    // Write error.
    Debug.Log(e);
}

XmlReader class有很多有用的方法。使用它。

看到这个:

var sb = new StringBuilder();

using (var reader = XmlReader.Create("test.xml"))
{
    while (reader.ReadToFollowing("part"))
    {
        var part = reader.ReadElementContentAsString();
        sb.Append("Part: ").AppendLine(part);

        reader.ReadToFollowing("number");
        var number = reader.ReadElementContentAsDouble();
        sb.Append("Number: ").Append(number).AppendLine();

        reader.ReadToFollowing("character");
        var character = reader.ReadElementContentAsString();
        sb.Append("Character: ").AppendLine(character);
    }
}

Console.WriteLine(sb);

Alexander 的回答很好,我只想使用 XDocument 添加示例,根据 Jon Skeet 的评论:

var sb = new StringBuilder();
var note = XDocument.Load("test.xml").Root.Descendants();            
foreach (var el in note)
{                
    sb.Append(el.Name).Append(": ").AppendLine(el.Value);               
}
Console.WriteLine(sb);