/[xmltv_parser]/trunk/libxmltv/Core/ExtraMetaData.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/ExtraMetaData.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libxmltv/Core/ExtraMetaData.cs	2013/03/10 11:35:26	105
+++ trunk/libxmltv/Core/ExtraMetaData.cs	2013/03/10 14:28:57	110
@@ -3,12 +3,86 @@
 using System.Linq;
 using System.Text;
 using libxmltv.Interfaces;
+using System.Xml.Linq;
+using System.IO;
+using System.Xml.Serialization;
+using System.Xml;
 
 namespace libxmltv.Core
 {
+    [Serializable]
     internal class ExtraMetaData : IExtraMetaData
     {
-        #region IExtraMetaData memebers
+        public ExtraMetaData() : this(string.Empty, string.Empty) { }
+        public ExtraMetaData(XElement node) : this()
+        {
+            //throw new NotImplementedException("ExtraMetaData from XElement as not been implemented");
+            if (node == null) { throw new ArgumentNullException("value", "cannot be null"); }
+            if (node.Name == null) { throw new ArgumentNullException("value.Name", "cannot be null"); }
+            this.Name = node.Name.ToString();
+            this.Value = XElementSerializer<string>.FromXElement(node); 
+        }
+        public ExtraMetaData(string name, string value)
+        {
+            this.Name = name;
+            this.Value = value;
+        }
+        public override string ToString()
+        {
+            return string.Format("{0}", this.Name);
+        }
+        private string _Name;
+        public string Name { get { return _Name; } set { _Name = value; } }
+        private string _Value;
+        public string Value { get { return _Value; } set { _Value = value; } }
+
+        public XElement AsXElement()
+        {
+            //throw new NotImplementedException("ExtraMetaData.AsXElement() has not been implemented");
+            return XElementSerializer<string>.ToXElement(Value); 
+        }
+
+        #region sub-classes
+        private static class XElementSerializer<TValue>
+        {
+            public static XElement ToXElement(TValue value)
+            {
+                if (value == null)
+                {
+                    throw new ArgumentNullException("value", string.Format("cannot convert a null '{0}' value to '{1}'.", typeof(TValue).Name, typeof(XElement).Name));
+                }
+                XElement element = null;
+                try
+                {
+                    element = XElement.Parse(value.ToString());
+                }
+                catch (Exception ex)
+                {
+                    throw ex;
+                }
+
+
+
+                return element;
+            }
+            public static TValue FromXElement(XElement node)
+            {
+                if (node == null)
+                {
+                    throw new ArgumentNullException("node", string.Format("cannot convert a null '{0}' value to {1}.", typeof(XElement).Name, typeof(TValue).Name));
+                }
+                TValue value = default(TValue);                
+                object raw_object = new object();
+                using (var ms = new MemoryStream())
+                {
+                    using (var tw = XmlWriter.Create(ms)) { node.WriteTo(tw); tw.Flush(); tw.Close(); }
+                    ms.Seek(0, SeekOrigin.Begin);
+                    using (var sr = new StreamReader(ms)) { raw_object = sr.ReadToEnd(); }
+                }
+                value = (TValue)Convert.ChangeType(raw_object, typeof(TValue));
+                return value;
+            }
+        }
         #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22