使用XmlReader读取xml文件

 

books.xml
   
     
<? xml version="1.0" encoding="utf-8" ?>
<!-- sample XML fragment -->
< bookstore >
< book genre ='novel' ISBN ='10-861003-324' >
< title > The Handmaid's Tale </ title >
< price > 19.95 </ price >
</ book >
< book genre ='novel' ISBN ='1-861001-57-5' >
< title > Pride And Prejudice </ title >
< price > 24.95 </ price >
</ book >
</ bookstore >

 

 

代码: 这个程序一本一本的读出所有的书

 

代码
   
     
static void TestReadOuterXml()
{
XmlReaderSettings settings
= new XmlReaderSettings();
settings.IgnoreWhitespace
= true ;

using (XmlReader reader = XmlReader.Create( " books.xml " )) {

// Moves the reader to the root element.
reader.MoveToContent();

// Moves to book node.
reader.Read();

while (reader.IsStartElement()) {
Console.WriteLine(
" Read the book using ReadOuterXml... " );
Console.WriteLine(reader.ReadOuterXml());
};
}
Console.ReadLine();

}

 

 

 

你可能感兴趣的:(reader)