Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace XmlTest2
- {
- class Program
- {
- static void Main(string[] args)
- {
- var _doc = XDocument.Load(@"XMLFile1.xml");
- QuestTargets q = new QuestTargets(_doc);
- var list = q.GetElementsByClass<Targets>();
- foreach (var p in list)
- {
- Console.WriteLine(p.grouping.name);
- }
- }
- }
- }
- public class QuestTargets : XmlElementHelper
- {
- public QuestTargets(string fileName) : base(fileName)
- {
- Parent = "targets";
- }
- public QuestTargets(XDocument doc) : base(doc)
- {
- Parent = "targets";
- }
- }
- public class Targets
- {
- public Grouping grouping;
- public Protounit protounit;
- };
- public class Grouping
- {
- public string cooponly { get; set; }
- public string forcetocenter { get; set; }
- public string name { get; set; }
- public string count { get; set; }
- public string team { get; set; }
- public string area { get; set; }
- public string groupfilename { get; set; }
- public string rottype { get; set; }
- public string rotateunitorientations { get; set; }
- }
- public class Overrides
- {
- public string displaynameid { get; set; }
- public string parent { get; set; }
- public string visualscale { get; set; }
- public string maxhitpoints { get; set; }
- public string initialhitpoints { get; set; }
- public string maxvelocity { get; set; }
- public string icon { get; set; }
- public string soundfile { get; set; }
- public string convertresist { get; set; }
- }
- public class Protounit
- {
- public string cooponly { get; set; }
- public string forcetocenter { get; set; }
- public string name { get; set; }
- public string protoname { get; set; }
- public string count { get; set; }
- public string team { get; set; }
- public string area { get; set; }
- public Overrides overrides { get; set; }
- public string usequesttargetindicator { get; set; }
- }
- public abstract class XmlElementHelper
- {
- private readonly XDocument _doc;
- private string _parent;
- private string _child;
- protected XmlElementHelper(string fileName)
- {
- _doc = XDocument.Load(fileName);
- }
- protected XmlElementHelper(XDocument doc)
- {
- _doc = doc;
- }
- public string Parent { get => _parent; set => _parent = value; }
- public T[] GetElementsByClass<T>() where T : class, new()
- {
- Assembly asm = Assembly.GetExecutingAssembly();
- var typeName = typeof(T).Name;
- var type = asm.GetTypes().First(t => t.Name == typeName);
- if (type == null) throw new ArgumentNullException(nameof(type));
- return _doc.Descendants(_parent)
- .Select(item =>
- {
- var p = Activator.CreateInstance(type) as T;
- foreach (var prop in item.Descendants())
- {
- var pi = p.GetType().GetProperty(prop.Name.LocalName);
- if (pi == null) continue;
- object newVal = Convert.ChangeType(prop.Value, pi.PropertyType);
- pi.SetValue(p, newVal, null);
- }
- return p;
- }).ToArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment