XmlReader.ReadToFollowing方法

时间:2014-05-06 12:20:55

标签: c# xml xml-parsing

我正在尝试从xml文件中读取一些部分,但它只是先读取,为什么?

这是代码:

using (XmlReader reader = XmlReader.Create("settings.xml"))
{
     if(reader.ReadToFollowing("LastProcessingId"))
         Console.Write(reader.ReadElementContentAsInt());
     if(reader.ReadToFollowing("TotalNumberFolders"))
         Console.Write(reader.ReadElementContentAsInt());
     if(reader.ReadToFollowing("LastAccessTime"))
         Console.Write(reader.ReadElementContentAsDateTime());
}

似乎第二次调用reader.ReadToFollowing在调用此行后不起作用 Console.Write(reader.ReadElementContentAsInt());

xml文件是:

<Settings>
  <LastProcessingId>3</LastProcessingId>
  <TotalNumberFolders>20</TotalNumberFolders>
  <LastAccessTime>2014-05-06T00:00:00+04:00</LastAccessTime>
</Settings>

4 个答案:

答案 0 :(得分:1)

这是为我工作的代码

using (XmlReader reader = XmlReader.Create("settings.xml"))
{
    reader.MoveToContent();
    while(reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element)
        {
            Console.WriteLine("{0}: ",reader.Name);
            // Next read will contain text.
            if (reader.Read()) Console.WriteLine("{0}: ", reader.Value);
        }                    
    }                
}

答案 1 :(得分:1)

一年过去了,但没有人真正回答OP的问题。 ReadToFollowing应该用于查找第一个节点,然后使用ReadToNextSibling应该用作described here。例如:

using (XmlReader reader = XmlReader.Create("books.xml")) {
    reader.ReadToFollowing("book");
    do {
       Console.WriteLine("ISBN: {0}", reader.GetAttribute("ISBN")); 
    } while (reader.ReadToNextSibling("book"));
}

答案 2 :(得分:0)

我通常会看到带有Read和switch语句的示例。我使用ReadToFollowing创建了一个工作示例:

books.xml文件:

<bookstore>
    <book genre='Science Fiction'>
        <title>Dune</title>
        <author>Frank Herbert</author>
        <price>8.99</price>
    </book>
    <book genre='Novel'>
        <title>Old Goriot</title>
        <author>Honoré de Balzac</author>
        <price>9.0</price>
    </book>
</bookstore>

C#代码示例:

using System;
using System.IO;
using System.Text;
using System.Xml;

namespace Simple
{
    class Program
    {
        static void Main(string[] args)
        {
            using var reader = XmlReader.Create("books.xml");

            reader.ReadToFollowing("book");

            do
            {
                reader.MoveToFirstAttribute();
                Console.WriteLine($"genre: {reader.Value}");

                reader.ReadToFollowing("title");
                Console.WriteLine($"title: {reader.ReadElementContentAsString()}");

                reader.ReadToFollowing("author");
                Console.WriteLine($"author: {reader.ReadElementContentAsString()}");

                reader.ReadToFollowing("price");
                Console.WriteLine($"price: {reader.ReadElementContentAsString()}");

                Console.WriteLine("-------------------------");

            } while (reader.ReadToFollowing("book"));
        }
    }
}

输出:

$ dotnet run
genre: Science Fiction
title: Dune
author: Frank Herbert
price: 8.99
-------------------------
genre: Novel
title: Old Goriot
author: Honoré de Balzac
price: 9.0
-------------------------

答案 3 :(得分:-2)

阅读所有节点使用:

            while (reader.Read())
            {

            }

但我使用Linq读取XML:

<customers>
  <customer id="84">
    <name value="Sample Name" />
  </customer>

  <customer id="89">
    <name value="Sample Name 2" />
  </customer>

  <customer id="80">
    <name value="Sample Name 3" />
  </customer>
</customers>

使用此代码:

XmlNode searched = null;
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Temporary\customers.xml");

foreach (XmlNode node in doc.SelectNodes("/customers/customer"))
{


}

OR

XElement main = XElement.Load(@"D:\Temporary\customers.xml");

            IEnumerable<XElement> searched =
                from c in main.Elements("customer")
                select c;