Advertisement
Shimmy

Untitled

Oct 10th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.   public class Program
  9.   {
  10.     const string Xml = @"<?xml version='1.0' encoding='utf-16'?>
  11. <ChordType name='C Minor' priority='Important'>
  12.  <description>C minor</description>
  13.  <abbreviations>
  14.    <abbr category='General'>m</abbr>
  15.    <abbr category='Jazz'>-</abbr>
  16.    <abbr category='Full'>min</abbr>
  17.  </abbreviations>
  18.  <intervals>
  19.    <interval quality='Perfect' number='Unison' />
  20.    <interval quality='Minor' number='Third' />
  21.    <interval quality='Perfect' number='Fifth' />
  22.  </intervals>
  23. </ChordType>";
  24.  
  25.     static void Main(string[] args)
  26.     {
  27.       ChordType ct;
  28.       var serializer = new XmlSerializer(typeof(ChordType));
  29.       using (var sr = new StringReader(Xml))
  30.         ct = (ChordType)serializer.Deserialize(sr);
  31.  
  32.     }
  33.  
  34.     public class ChordType : IXmlSerializable
  35.     {
  36.  
  37.       public System.Xml.Schema.XmlSchema GetSchema()
  38.       {
  39.         return null;
  40.       }
  41.  
  42.       public void ReadXml(XmlReader reader)
  43.       {
  44.         Console.WriteLine("Name: {0}", reader.GetAttribute("name"));
  45.         Console.WriteLine("Priority: {0}", reader.GetAttribute("priority"));
  46.         if (reader.ReadToFollowing("description"))
  47.           Console.WriteLine("Description: {0}", reader.ReadElementContentAsString());
  48.  
  49.         if (reader.Name != "abbreviations")
  50.           throw new InvalidOperationException("Abbreviations element missing.");
  51.         if (!reader.ReadToDescendant("abbr"))
  52.           throw new InvalidOperationException("At least one abbreviation element is required.");
  53.         var i = 0;
  54.         do
  55.           Console.WriteLine("Abbreviation {0} - Category: {1}, Abbr: {2}", ++i, reader.GetAttribute("category"), reader.ReadElementContentAsString());
  56.         while (reader.ReadToNextSibling("abbr"));
  57.  
  58.  
  59.         if (!reader.ReadToFollowing("intervals"))
  60.           //here's where the code fails
  61.           throw new InvalidOperationException("Intervals element missing.");
  62.         i = 0;
  63.         do
  64.           Console.WriteLine("Interval {0} - quality: {1}, abbr: {2}", ++i, reader.GetAttribute("quality"), reader.GetAttribute("number"));
  65.         while (reader.ReadToNextSibling("interval"));
  66.       }
  67.  
  68.       public void WriteXml(System.Xml.XmlWriter writer)
  69.       {
  70.         throw new NotImplementedException();
  71.       }
  72.     }
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement