using System; using System.IO; using System.Text; class Class1{ static void Main(string[] args) { byte[] byData = new byte[100]; char[] charData = new Char[100]; try { FileStream aFile = new FileStream("practice.txt",FileMode.Open); aFile.Seek(55,SeekOrigin.Begin); aFile.Read(byData,0,100); } catch(IOException e) { Console.WriteLine("An IO exception has been thrown!"); Console.WriteLine(e.ToString()); Console.ReadLine(); return; } Decoder d = Encoding.UTF8.GetDecoder(); d.GetChars(byData, 0, byData.Length, charData, 0); Console.WriteLine(charData); return; } }
Working with the BinaryReader Class
using System; using System.IO; class MainClass { static public void Main() { FileStream BinaryFile = new FileStream("test.dat", FileMode.Create, FileAccess.ReadWrite); BinaryReader Reader = new BinaryReader(BinaryFile); BinaryWriter Writer = new BinaryWriter(BinaryFile); Writer.Write('a'); Writer.Write(123); Writer.Write(456.789); Writer.Write("test string"); char ReadCharacter; double ReadDouble; int ReadInteger; string ReadString; BinaryFile.Seek(0, SeekOrigin.Begin); ReadCharacter = Reader.ReadChar(); ReadInteger = Reader.ReadInt32(); ReadDouble = Reader.ReadDouble(); ReadString = Reader.ReadString(); Console.WriteLine("Character: {0}", ReadCharacter); Console.WriteLine("Integer: {0}", ReadInteger); Console.WriteLine("Double: {0}", ReadDouble); Console.WriteLine("String: {0}", ReadString); } }
Working with the BinaryWriter Class
using System; using System.IO; class MainClass { static public void Main() { FileStream BinaryFile = new FileStream("test.dat", FileMode.Create, FileAccess.ReadWrite); BinaryWriter Writer = new BinaryWriter(BinaryFile); Writer.Write('a'); Writer.Write(123); Writer.Write(456.789); Writer.Write("test string"); } }
Obtaining Restricted Elements Without Reaching While Ordering and Using Query Expression Syntax
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); IEnumerable<XElement> elements = from e in xDocument.Descendants("Book") where ((string)e.Attribute("type")) != "Illustrator" orderby ((string)e.Element("LastName")) select e; foreach (XElement element in elements) { Console.WriteLine("Element: {0} : value = {1}",element.Name, element.Value); } } }
Obtaining Restricted Elements Without Reaching
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); IEnumerable<XElement> elements = xDocument.Descendants("Book") .Where(e => ((string)e.Element("FirstName")) == "C"); foreach (XElement element in elements) { Console.WriteLine("Element: {0} : value = {1}",element.Name, element.Value); } } }
Obtaining Elements Without Reaching
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); IEnumerable<XElement> elements = xDocument.Descendants("Book"); foreach (XElement element in elements) { Console.WriteLine("Element: {0} : value = {1}",element.Name, element.Value); } } }
Adding a Node to the End of the Specified Node's Child Nodes with Add
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); xDocument.Element("Books").Add( new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "E"), new XElement("LastName", "B"))); Console.WriteLine(xDocument); } }