2014-09-05 37 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元素。