2014-09-05 32 views
0

说我有以下XML:XML反序列

<rootelement> 
    <childone>val1</childone> 
    <childtwo>val2</childtwo> 
    <childthree>val3</childthree> 
</rootelement> 

反序列化到一个对象,我通常会是这样的:

public class rootelement{ 
    public string childone,childtwo,childthree; 
} 

这所有的作品,但现在我想知道是否可以将子节点名称存储在一个数组或其他东西中,以便我可以更容易地管理我的字段,并使用此数组来填充ListKeyValuePair中的密钥,例如:

string[] fieldnames={"childone","childtwo","childthree"}; 
List<KeyValuePair<string, string>> fields=new List<KeyValuePair<string, string>>(); 
for(int i=0;i<fieldnames.Length;i++){ 
    fields.Add(new KeyValuePair<string,string>(fieldnames[i],"")); 
} 

最后一步是反序列化来填充值。 具体来说,它不一定是ListKeyValuePair,我可以应用相同概念的任何东西都可以工作。

是这样的可能吗?如果是这样,请以示例帮助我。

回答

1

我最终什么事做了以下内容:

public class MyXmlRoot{ 

private string[] allowedTags={"tagA","tagB","tagC"}; 

[XmlAnyElement] 
public List<XmlElement> children = new List<XmlElement>(); //populated after serialization 

public string GetValueByKey(string key){ 
    return children.Find(k => k.Name == key).InnerText; 
} 

public void UseTags(){ 
    for(int i=0;i<allowedTags.Length;i++){ 
     Console.WriteLine(allowedTags[i]+" = "+GetValueByKey(allowedTags[i])); 
    } 
} 

} 
0

如果我的理解是正确的,则需要手动反序列化。 的optoins做到这一点的一个使用的XDocument:

using System; 
using System.Collections.Generic; 
using System.Reflection; 
using System.Xml.Linq; 
using System.Xml.XPath; 

class Program 
{ 
    class Children 
    { 
     public string one { get; set; } 
     public string two { get; set; } 
    } 

    static void Main(string[] args) 
    { 
     string xmlstr = @"<?xml version=""1.0"" encoding=""utf-8""?> 
<rootelement> 
    <childone>val1</childone> 
    <childtwo>val2</childtwo> 
    <childthree>val3</childthree> 
    <children1> 
    <children2> 
     <one>val1-1</one> 
     <two>val1-2</two> 
     <three>val1-3</three> 
    </children2> 
    </children1> 
</rootelement>"; 

     XDocument xml = XDocument.Parse(xmlstr); 

     string[] fieldnames = { "childone", "childtwo", "childthree" }; 
     List<KeyValuePair<string, string>> fields = new List<KeyValuePair<string, string>>(); 
     foreach (string i in fieldnames) 
     { 
      XElement elem = xml.Root.XPathSelectElement(i); 
      if (elem != null) 
      { 
       fields.Add(new KeyValuePair<string, string>(i, elem.Value)); 
      } 
     } 

     // Debug 
     foreach (KeyValuePair<string, string> f in fields) 
     { 
      Console.WriteLine(f); 
     } 

     // Try to fill specific object's properties with using reflection 

     string parentPath = "children1/children2"; 
     string[] names = { "one", "two", "three" }; 
     Children childrenFields = new Children(); 
     foreach (var name in names) 
     { 
      PropertyInfo prop = typeof(Children).GetProperty(name); 
      if (prop != null) 
      { 
       XElement elem = xml.Root.XPathSelectElement(parentPath + "/" + name); 
       if (elem != null) 
       { 
        prop.SetValue(childrenFields, elem.Value, null); 
       } 
      } 
     } 

     // Debug 
     Console.WriteLine("Children one: {0}, two: {1}", 
      childrenFields.one, childrenFields.two); 
    } 
} 

该代码使用System.Xml.XPath.Extensions.XPathSelectElement扩展方法通过使用XPath支持更复杂的XML文档,在我的例子,如children1元素找到目标XML元素。