Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Xml;
- using System.Xml.Serialization;
- namespace ConsoleApplication1
- {
- public class Program
- {
- const string Xml = @"<?xml version='1.0' encoding='utf-16'?>
- <ChordType name='C Minor' priority='Important'>
- <description>C minor</description>
- <abbreviations>
- <abbr category='General'>m</abbr>
- <abbr category='Jazz'>-</abbr>
- <abbr category='Full'>min</abbr>
- </abbreviations>
- <intervals>
- <interval quality='Perfect' number='Unison' />
- <interval quality='Minor' number='Third' />
- <interval quality='Perfect' number='Fifth' />
- </intervals>
- </ChordType>";
- static void Main(string[] args)
- {
- ChordType ct;
- var serializer = new XmlSerializer(typeof(ChordType));
- using (var sr = new StringReader(Xml))
- ct = (ChordType)serializer.Deserialize(sr);
- }
- public class ChordType : IXmlSerializable
- {
- public System.Xml.Schema.XmlSchema GetSchema()
- {
- return null;
- }
- public void ReadXml(XmlReader reader)
- {
- Console.WriteLine("Name: {0}", reader.GetAttribute("name"));
- Console.WriteLine("Priority: {0}", reader.GetAttribute("priority"));
- if (reader.ReadToFollowing("description"))
- Console.WriteLine("Description: {0}", reader.ReadElementContentAsString());
- if (reader.Name != "abbreviations")
- throw new InvalidOperationException("Abbreviations element missing.");
- if (!reader.ReadToDescendant("abbr"))
- throw new InvalidOperationException("At least one abbreviation element is required.");
- var i = 0;
- do
- Console.WriteLine("Abbreviation {0} - Category: {1}, Abbr: {2}", ++i, reader.GetAttribute("category"), reader.ReadElementContentAsString());
- while (reader.ReadToNextSibling("abbr"));
- if (!reader.ReadToFollowing("intervals"))
- //here's where the code fails
- throw new InvalidOperationException("Intervals element missing.");
- i = 0;
- do
- Console.WriteLine("Interval {0} - quality: {1}, abbr: {2}", ++i, reader.GetAttribute("quality"), reader.GetAttribute("number"));
- while (reader.ReadToNextSibling("interval"));
- }
- public void WriteXml(System.Xml.XmlWriter writer)
- {
- throw new NotImplementedException();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement