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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (show annotations) (download)
Sun Mar 10 14:28:57 2013 UTC (7 years, 7 months ago) by william
File size: 3295 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using libxmltv.Interfaces;
6 using System.Xml.Linq;
7 using System.IO;
8 using System.Xml.Serialization;
9 using System.Xml;
10
11 namespace libxmltv.Core
12 {
13 [Serializable]
14 internal class ExtraMetaData : IExtraMetaData
15 {
16 public ExtraMetaData() : this(string.Empty, string.Empty) { }
17 public ExtraMetaData(XElement node) : this()
18 {
19 //throw new NotImplementedException("ExtraMetaData from XElement as not been implemented");
20 if (node == null) { throw new ArgumentNullException("value", "cannot be null"); }
21 if (node.Name == null) { throw new ArgumentNullException("value.Name", "cannot be null"); }
22 this.Name = node.Name.ToString();
23 this.Value = XElementSerializer<string>.FromXElement(node);
24 }
25 public ExtraMetaData(string name, string value)
26 {
27 this.Name = name;
28 this.Value = value;
29 }
30 public override string ToString()
31 {
32 return string.Format("{0}", this.Name);
33 }
34 private string _Name;
35 public string Name { get { return _Name; } set { _Name = value; } }
36 private string _Value;
37 public string Value { get { return _Value; } set { _Value = value; } }
38
39 public XElement AsXElement()
40 {
41 //throw new NotImplementedException("ExtraMetaData.AsXElement() has not been implemented");
42 return XElementSerializer<string>.ToXElement(Value);
43 }
44
45 #region sub-classes
46 private static class XElementSerializer<TValue>
47 {
48 public static XElement ToXElement(TValue value)
49 {
50 if (value == null)
51 {
52 throw new ArgumentNullException("value", string.Format("cannot convert a null '{0}' value to '{1}'.", typeof(TValue).Name, typeof(XElement).Name));
53 }
54 XElement element = null;
55 try
56 {
57 element = XElement.Parse(value.ToString());
58 }
59 catch (Exception ex)
60 {
61 throw ex;
62 }
63
64
65
66 return element;
67 }
68 public static TValue FromXElement(XElement node)
69 {
70 if (node == null)
71 {
72 throw new ArgumentNullException("node", string.Format("cannot convert a null '{0}' value to {1}.", typeof(XElement).Name, typeof(TValue).Name));
73 }
74 TValue value = default(TValue);
75 object raw_object = new object();
76 using (var ms = new MemoryStream())
77 {
78 using (var tw = XmlWriter.Create(ms)) { node.WriteTo(tw); tw.Flush(); tw.Close(); }
79 ms.Seek(0, SeekOrigin.Begin);
80 using (var sr = new StreamReader(ms)) { raw_object = sr.ReadToEnd(); }
81 }
82 value = (TValue)Convert.ChangeType(raw_object, typeof(TValue));
83 return value;
84 }
85 }
86 #endregion
87 }
88 }

  ViewVC Help
Powered by ViewVC 1.1.22