PROXiCiDE

Program.cs

Jun 21st, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml.Linq;
  9.  
  10. namespace XmlTest2
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             var _doc = XDocument.Load(@"XMLFile1.xml");
  17.             QuestTargets q = new QuestTargets(_doc);
  18.  
  19.             var list = q.GetElementsByClass<Targets>();
  20.             foreach (var p in list)
  21.             {
  22.                 Console.WriteLine(p.grouping.name);
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. public class QuestTargets : XmlElementHelper
  29. {
  30.     public QuestTargets(string fileName) : base(fileName)
  31.     {
  32.         Parent = "targets";
  33.     }
  34.  
  35.     public QuestTargets(XDocument doc) : base(doc)
  36.     {
  37.         Parent = "targets";
  38.     }
  39. }
  40.  
  41. public class Targets
  42. {
  43.     public Grouping grouping;
  44.     public Protounit protounit;
  45. };
  46.  
  47. public class Grouping
  48. {
  49.     public string cooponly { get; set; }
  50.     public string forcetocenter { get; set; }
  51.     public string name { get; set; }
  52.     public string count { get; set; }
  53.     public string team { get; set; }
  54.     public string area { get; set; }
  55.     public string groupfilename { get; set; }
  56.     public string rottype { get; set; }
  57.     public string rotateunitorientations { get; set; }
  58. }
  59.  
  60. public class Overrides
  61. {
  62.     public string displaynameid { get; set; }
  63.     public string parent { get; set; }
  64.     public string visualscale { get; set; }
  65.     public string maxhitpoints { get; set; }
  66.     public string initialhitpoints { get; set; }
  67.     public string maxvelocity { get; set; }
  68.     public string icon { get; set; }
  69.     public string soundfile { get; set; }
  70.     public string convertresist { get; set; }
  71. }
  72.  
  73. public class Protounit
  74. {
  75.     public string cooponly { get; set; }
  76.     public string forcetocenter { get; set; }
  77.     public string name { get; set; }
  78.     public string protoname { get; set; }
  79.     public string count { get; set; }
  80.     public string team { get; set; }
  81.     public string area { get; set; }
  82.     public Overrides overrides { get; set; }
  83.     public string usequesttargetindicator { get; set; }
  84. }
  85.  
  86. public abstract class XmlElementHelper
  87. {
  88.     private readonly XDocument _doc;
  89.     private string _parent;
  90.     private string _child;
  91.  
  92.     protected XmlElementHelper(string fileName)
  93.     {
  94.         _doc = XDocument.Load(fileName);
  95.     }
  96.  
  97.     protected XmlElementHelper(XDocument doc)
  98.     {
  99.         _doc = doc;
  100.     }
  101.  
  102.     public string Parent { get => _parent; set => _parent = value; }
  103.  
  104.     public T[] GetElementsByClass<T>() where T : class, new()
  105.     {
  106.         Assembly asm = Assembly.GetExecutingAssembly();
  107.         var typeName = typeof(T).Name;
  108.         var type = asm.GetTypes().First(t => t.Name == typeName);
  109.         if (type == null) throw new ArgumentNullException(nameof(type));
  110.  
  111.         return _doc.Descendants(_parent)
  112.             .Select(item =>
  113.             {
  114.                 var p = Activator.CreateInstance(type) as T;
  115.                 foreach (var prop in item.Descendants())
  116.                 {
  117.                     var pi = p.GetType().GetProperty(prop.Name.LocalName);
  118.                     if (pi == null) continue;
  119.  
  120.                     object newVal = Convert.ChangeType(prop.Value, pi.PropertyType);
  121.                     pi.SetValue(p, newVal, null);
  122.                 }
  123.  
  124.                 return p;
  125.             }).ToArray();
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment