Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Chuck has a good approach. Sometimes, you don't want to retain the reference to the underlying xml data structure in the model object you are loading. In this case, I often use a pattern like this:</p> <pre><code>public interface IXmlReadable { void Clear(); void Read(XPathNavigator xmlNav); } public class ModelBase : IXmlReadable { public void Clear() { DoClear(); } public void Read(XPathNavigator xmlNav) { DoRead(xmlNav); } protected virtual void DoClear() { throw new NotImplementedException(); } protected virtual void DoRead(XPathNavigator xmlNav) { throw new NotImplementedException(); } } </code></pre> <p>Read can be overloaded to accept <code>XmlDocument</code>, <code>XmlNode</code>, <code>XElement</code>, etc.</p> <p>Now you can implement specific models.</p> <pre><code>public sealed class Order : ModelBase { public Order() { } public string OrderNumber { get; private set; } protected override void DoClear() { OrderNumber = string.Empty; } protected override void DoRead(XPathNavigator xmlNav) { DoClear(); XPathNavigator node; node = xmlNav.SelectSingleNode("OrderNumber"); if (node != null) OrderNumber = node.InnerXml; // implement other properties here } } </code></pre> <p>With the XPathDocument and XPathNavigator approach, you can then do something like this:</p> <pre><code>XPathDocument xml = new XPathDocument(@"C:\onlinesales\neworders.xml"); xmlNav = xml.CreateNavigator(); XPathNodeIterator iterator = xmlNav.Select("Order"); while (iterator.MoveNext()) { Order order = new Order(); order.Read(iterator.Current); // do something with the Order - add to list or process } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload