2013-03-19 57 views
0

我试图写它使用反射,以获取属性并将其值设置在遍历的XElement的方法:导入XML到对象递归

可以说我有这样一类仅提供我的XML值要解析:

class XMLController 
{ 
    public string XML 
    { 
     get{ 
      return @"<FieldGroup name='People' count='20'> 
       <Fields> 
        <Field Name='Jon' LastName='McFly'/> 
        <Field Name='Michael' LastName='Jackson'/> 
       </Fields> 
      </FieldGroup>"; 
     } 
    } 
} 

这是我的对象怎么看起来像:

class FieldGroup 
{ 
    public string Name {get;set;} 
    public string Count {get;set;} 
    public IEnumerable<Field> Fields {get;set;} 
} 

class Field 
{ 
    public string Name {get;set;} 
    public string LastName {get;set;} 
} 

映射器方法遍历XElement和由于节点南es与匹配的对象的名称我认为这有助于多一点,但我还没有拿出一些真正有用的东西。我不想传递这个类型,但是,该方法几乎可以处理传入的所有XML格式相同的XML。

它只知道XML节点和属性是匹配的名称。

这是我做了什么,但并没有真正的工作:

class XMLObjectMapper 
{ 
    public T Map<T>(XElement element) where T: class, new() 
    { 
     T entity = (T) Activator.CreateInstance(typeof(T)); 
     if(element.HasAttributes) 
     { 
      MapXMLAttributesToObject<T>(element,entity); 
     } 
     if(element.HasElements) 
     { 
      foreach (var childElement in element.Elements()) 
      { 
       //if the child element has child elements as well, we know this is a collection. 
       if(childElement.HasElements) 
       { 
        var property = GetProperty<T>(childElement.Name.LocalName); 
        property.SetValue(entity,new List<property.PropertyType>()); 
        Map<T>(childElement); 

       } 
       else 
       { 
        var property = GetProperty<T>(childElement.Name.LocalName); 
        var type = Activator.CreateInstance(property.PropertyType); 
        type.Dump(); 
       } 
      } 
     } 
     return entity; 
    } 

    private void MapXMLAttributesToObject<T>(XElement element, T entity) 
    { 
     foreach(XAttribute attribute in element.Attributes()) 
     { 
      var property = GetProperty<T>(attribute.Name.LocalName); 
      property.SetValue(entity,attribute.Value); 
     } 
    } 

    private PropertyInfo GetProperty<T>(string propertyName) 
    { 
     return typeof(T).GetProperty(propertyName,BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); 
    } 
} 
+0

“XmlSerializer”有问题吗? – 2013-03-19 04:06:03

+0

不幸的是,我不需要灵活性。 – Tarik 2013-03-19 04:17:55

回答

2

你在正确的轨道上,但你已经注意到了,你有一些错误。

由于无法使用值(property.PropertyType)代替类型名称,因此下面的一段代码无法编译。 C#是一种静态类型语言,所以类型在编译时是已知的,而不是变量是:

new List<property.PropertyType>() 

但是,如果你使用反射,你可以在运行时选择的类型。我们可以这样做,而不是:

Activator.CreateInstance(typeof(List<>).MakeGenericType(collectionElementType)) 

,你必须是你不能只是调用Map<T>(childElement)的另一个问题。首先,T不是正确的类型 - 它是父元素的类型,而不是孩子的类型。其次,孩子实际上是一个集合,并且Map<T>不知道如何处理集合,而只知道单个对象。我们必须遍历子元素,映射到每个元素上(使用集合中元素的类型调用Map<T> - 在您的示例中,Map<Field),然后将它们全部添加到集合中。我做了你的Map<T>的新版本的作品:

public T Map<T>(XElement element) where T : class, new() 
{ 
    T entity = (T)Activator.CreateInstance(typeof(T)); 
    if (element.HasAttributes) 
    { 
     MapXMLAttributesToObject<T>(element, entity); 
    } 
    if (element.HasElements) 
    { 
     foreach (var childElement in element.Elements()) 
     { 
      var property = GetProperty<T>(childElement.Name.LocalName); 
      // If the child element has child elements as well, we know this is a collection. 
      if (childElement.HasElements) 
      { 
       // Assume collections are of type IEnumerable<T> or List<T> 
       var collectionElementType = property.PropertyType.GetGenericArguments()[0]; 
       // var collectionValue = new List<collectionElementType>() 
       var collectionValue = Activator.CreateInstance(typeof(List<>).MakeGenericType(collectionElementType)); 
       foreach (var grandchildElement in childElement.Elements()) 
       { 
        // var collectionElement = this.Map<collectionElementType>(grandchildElement); 
        var collectionElement = this.GetType().GetMethod("Map").MakeGenericMethod(collectionElementType).Invoke(this, new object[] { grandchildElement }); 
        collectionValue.GetType().GetMethod("Add").Invoke(collectionValue, new object[] { collectionElement }); 
       } 
       property.SetValue(entity, collectionValue, null); 
      } 
      else 
      { 
       // I'm not sure what this should do -- this case doesn't happen in your example. 
       throw new NotImplementedException(); 
      } 
     } 
    } 
    return entity; 
} 

这当然需要更多的错误处理,和我假设你想干的事在我扔了NotImplementedException的情况下非常有用。但是,它适用于您的示例。

+0

你救了我的命。谢谢! – Tarik 2013-03-19 16:06:49