Csharp/C Sharp by API/System.Xml/XmlReader

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

XmlReader.EOF

<source lang="csharp"> using System; using System.Data; using System.Data.SqlClient; using System.Xml; class ExecuteXmlReader {

   public static void Main() {
       SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
       SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
       mySqlCommand.rumandText =
         "SELECT TOP 5 ProductID, ProductName, UnitPrice " +
         "FROM Products " +
         "ORDER BY ProductID " +
         "FOR XML AUTO";
       mySqlConnection.Open();
       XmlReader myXmlReader = mySqlCommand.ExecuteXmlReader();
       myXmlReader.Read();
       while (!myXmlReader.EOF) {
           Console.WriteLine(myXmlReader.ReadOuterXml());
       }
       myXmlReader.Close();
       mySqlConnection.Close();
   }

}


 </source>


XmlReader.Read()

<source lang="csharp">

using System; using System.Data; using System.Data.SqlClient; using System.Xml; class ExecuteXmlReader {

   public static void Main() {
       SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
       SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
       mySqlCommand.rumandText =
         "SELECT TOP 5 ProductID, ProductName, UnitPrice " +
         "FROM Products " +
         "ORDER BY ProductID " +
         "FOR XML AUTO";
       mySqlConnection.Open();
       XmlReader myXmlReader = mySqlCommand.ExecuteXmlReader();
       myXmlReader.Read();
       while (!myXmlReader.EOF) {
           Console.WriteLine(myXmlReader.ReadOuterXml());
       }
       myXmlReader.Close();
       mySqlConnection.Close();
   }

}


 </source>


XmlReader.ReadElementContentAsString

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Xml; using System.IO; class MainClass {

   static void Main(string[] args)
   {
       WebClient client = new WebClient();
       Stream rssFeedStream = client.OpenRead("http://yourRssFeedURL");
       XmlReader reader = XmlReader.Create(rssFeedStream);
       reader.MoveToContent();
       while (reader.ReadToFollowing("item"))
       {
           ProcessItem(reader.ReadSubtree());
       }
   }
   static void ProcessItem(XmlReader reader)
   {
       reader.ReadToFollowing("title");
       string title = reader.ReadElementContentAsString("title", reader.NamespaceURI);
       reader.ReadToFollowing("link");
       string link = reader.ReadElementContentAsString("link", reader.NamespaceURI);
       Console.WriteLine("{0}\n\t{1}", title, link);
   }

}


 </source>


XmlReader.ReadOuterXml()

<source lang="csharp"> using System; using System.Data; using System.Data.SqlClient; using System.Xml; class ExecuteXmlReader {

   public static void Main() {
       SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
       SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
       mySqlCommand.rumandText =
         "SELECT TOP 5 ProductID, ProductName, UnitPrice " +
         "FROM Products " +
         "ORDER BY ProductID " +
         "FOR XML AUTO";
       mySqlConnection.Open();
       XmlReader myXmlReader = mySqlCommand.ExecuteXmlReader();
       myXmlReader.Read();
       while (!myXmlReader.EOF) {
           Console.WriteLine(myXmlReader.ReadOuterXml());
       }
       myXmlReader.Close();
       mySqlConnection.Close();
   }

}


 </source>


XmlReader.ReadToFollowing

<source lang="csharp">

using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Xml; using System.IO; class MainClass {

   static void Main(string[] args)
   {
       WebClient client = new WebClient();
       Stream rssFeedStream = client.OpenRead("http://yourRssFeedURL");
       XmlReader reader = XmlReader.Create(rssFeedStream);
       reader.MoveToContent();
       while (reader.ReadToFollowing("item"))
       {
           ProcessItem(reader.ReadSubtree());
       }
   }
   static void ProcessItem(XmlReader reader)
   {
       reader.ReadToFollowing("title");
       string title = reader.ReadElementContentAsString("title", reader.NamespaceURI);
       reader.ReadToFollowing("link");
       string link = reader.ReadElementContentAsString("link", reader.NamespaceURI);
       Console.WriteLine("{0}\n\t{1}", title, link);
   }

}


 </source>