One approach is to not parse and use XmlSerialization (um, actually Deserialization).
To do this, you create class(es) that represents the xml schema. Then use XmlSerializer to deserialize the xml into an object.
Finally you read the object's properties and save to the database. The sample below skips the database saving part.
First, the Event class (which represents a single event xml entry).
Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace CG.EventXmlDeserialization
{
[XmlRoot(ElementName = "event", Namespace = "")]
[XmlType(AnonymousType = true, Namespace = "")]
public class Event
{
public static Event CreateFromXml(string xml)
{
var serializer = new XmlSerializer(typeof(Event));
using (var reader = new StringReader(xml))
{
return (Event)serializer.Deserialize(reader);
}
}
[XmlElement("site")]
public string Site { get; set; }
[XmlElement("eventid")]
public Guid EventId { get; set; }
[XmlElement("eventdata")]
public EventData Data { get; set; }
public Event()
{
Data = new EventData();
}
}
public class EventData
{
[XmlArray("currencies")]
[XmlArrayItem("currency")]
public Currency[] Currencies
{
set { if (value != null) { _currencies.AddRange(value); } }
get { return _currencies.ToArray(); }
}
public EventData()
{
_currencies = new List<Currency>();
}
private List<Currency> _currencies;
}
public class Currency
{
[XmlElement("isocode")]
public string IsoCode { get; set; }
[XmlElement("bankbuyrate")]
public decimal BankBuyRate { get; set; }
[XmlElement("banksellrate")]
public decimal BankSellRate { get; set; }
[XmlElement("STAFFbuyrate")]
public decimal StaffBuyRate { get; set; }
[XmlElement("STAFFsellrate")]
public decimal StaffSellRate { get; set; }
[XmlElement("displayname")]
public string DisplayName { get; set; }
[XmlElement("decimaldigits")]
public int DecimalDigits { get; set; }
}
}
Next, code for simulating the incoming event xml, deserializing it into an Event object, and then writing out the properties.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace CG.EventXmlDeserialization
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("CG.EventXmlDeserialization");
Console.WriteLine("Illustrates deserializing xml into an object\n");
// Simulate incoming xml string by reading the xml file
// and convert it into and xml string
var xmlDoc = new XmlDocument();
xmlDoc.Load("event.xml");
var xml = xmlDoc.InnerXml;
// Deserialize the xml into an Event object
var evt = Event.CreateFromXml(xml);
// Use the Event object's values
Console.WriteLine("{0,15} {1}", "EventId:", evt.EventId);
Console.WriteLine("{0,15} {1}", "Site:", evt.Site);
Console.WriteLine("\n{0,15}", "Currencies:");
foreach (var currency in evt.Data.Currencies)
{
Console.WriteLine("{0,20} {1}", "IsoCode:", currency.IsoCode);
Console.WriteLine("{0,20} {1}", "BankBuyRate:", currency.BankBuyRate);
Console.WriteLine("{0,20} {1}", "BankSellRate:", currency.BankSellRate);
Console.WriteLine("{0,20} {1}", "StaffBuyRate:", currency.StaffBuyRate);
Console.WriteLine("{0,20} {1}", "StaffSellRate:", currency.StaffSellRate);
Console.WriteLine("{0,20} {1}", "DisplayName:", currency.DisplayName);
Console.WriteLine("{0,20} {1}", "DecimalDigits:", currency.DecimalDigits);
}
}
}
}
Sample Output:
Code:
CG.EventXmlDeserialization
Illustrates deserializing xml into an object
EventId: 6f98cb30-17de-48f8-99d9-7660f612eccb
Site: FLEX-CUBE
Currencies:
IsoCode: USD
BankBuyRate: 3.65
BankSellRate: 3.695
StaffBuyRate: 3.673
StaffSellRate: 3.673
DisplayName: USD
DecimalDigits: 4
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.