2009-11-10 143 views
7

我不需要編輯任何XML文件或任何東西,這僅用於讀取和解析。將XML文檔轉換爲字典

我希望能夠將XML文檔作爲字典處理,如:username = doc["username"];,但我無法找到如何「轉換」文檔。我也遇到了重複鍵名的問題,但是可以通過在每個值後面加上1,2來避免這個問題。使得它也易於循環。

這可能嗎?將(分析的)XML文檔視爲字典?


答到邁赫達德: 它從時間不同時間,這取決於來自用戶的請求。如果用戶請求x,那麼這將是:

<xml> 
    <test>foo</test> 
    <bar>123</bar> 
    <username>foobar</username> 
</xml> 

但如果他要求y,它會像

<xml> 
    <ammount>1000</ammount> 
    <mail>[email protected]</mail> 
    <username>foobar</username> 
</xml> 

最好的是,如果這樣的:

<xml> 
<mengde>100</mengde> 
<type>3</type> 
<mail>foo</mail> 
<crypt>bar</crypt> 
<username>bar</username> 
</xml>" 

可以解析然後作爲doc["mengde"]等被訪問等。

+0

什麼是XML文檔的結構? – 2009-11-10 18:40:37

+0

你想如何處理子文檔?什麼文檔[ 「富」 在''回報?你需要解釋你想如何訪問doc/foo/a,以便我們提供幫助。 – jmucchiello 2009-11-10 20:01:47

+0

包含節點的節點將被插入,我只使用包含文本的節點。在你的例子中:x,a和b。 – Phoexo 2009-11-10 20:31:48

回答

12

你可以使用LINQ到XML來你想要什麼(如果我理解你想要的)

string data = "<data><test>foo</test><test>foobbbbb</test><bar>123</bar><username>foobar</username></data>"; 

XDocument doc = XDocument.Parse(data); 
Dictionary<string, string> dataDictionary = new Dictionary<string, string>(); 

foreach (XElement element in doc.Descendants().Where(p => p.HasElements == false)) { 
    int keyInt = 0; 
    string keyName = element.Name.LocalName; 

    while (dataDictionary.ContainsKey(keyName)) { 
     keyName = element.Name.LocalName + "_" + keyInt++; 
    } 

    dataDictionary.Add(keyName, element.Value); 
} 
+0

謝謝!作爲例外,工作嚴格:) – Phoexo 2009-11-10 20:37:03

4

XML數據

<?xml version="1.0" encoding="UTF-8"?> 
<data> 
    <resource key="123">foo</resource> 
    <resource key="456">bar</resource> 
    <resource key="789">bar</resource> 
</data> 

轉換碼

string s = "<data><resource key=\"123\">foo</resource><resource key=\"456\">bar</resource><resource key=\"789\">bar</resource></data>"; 
XmlDocument xml = new XmlDocument(); 
xml.LoadXml(s); 
XmlNodeList resources = xml.SelectNodes("data/resource"); 
SortedDictionary<string,string> dictionary = new SortedDictionary<string,string>(); 
foreach (XmlNode node in resources){ 
    dictionary.Add(node.Attributes["key"].Value, node.InnerText); 
} 

這個問題是以前問這裏,所以你可以找到在這個環節中所有的答案:

convert xml to sorted dictionary

希望它有幫助。

+0

Ty,但我寧願能夠使用標籤名稱而不是爲所有內容添加屬性。 – Phoexo 2009-11-10 19:23:19

0

有一定有比這個爛攤子一個更簡單的方法?這也是惹人注目的,因爲我只能發現孩子的孩子。

string s = @" 
<xml> 
<mengde>100</mengde> 
<type>2</type> 
<foo>bar</foo> 
</xml>"; 

XmlDocument xml = new XmlDocument(); 
xml.LoadXml(s); 
SortedDictionary<string, string> dictionary = new SortedDictionary<string, string>(); 
foreach (XmlNode node in xml) 
{ 
    if (node.NodeType == XmlNodeType.Element && node.HasChildNodes) 
    { 
     foreach (XmlNode node2 in node) 
     { 
      if (node2.NodeType == XmlNodeType.Element && node2.HasChildNodes) 
      { 
       foreach (XmlNode node3 in node2) 
       { 
        if (node3.NodeType == XmlNodeType.Element && node3.HasChildNodes) 
        { 
         foreach (XmlNode node4 in node3) 
         { 
          if (node4.NodeType == XmlNodeType.Element && node4.HasChildNodes) 
          { 
           foreach (XmlNode node5 in node4) 
           { 
            dictionary.Add(node5.ParentNode.Name, node5.InnerText); 
           } 
          } 
          else 
          { 
           dictionary.Add(node4.ParentNode.Name, node4.InnerText); 
          } 
         } 
        } 
        else 
        { 
         dictionary.Add(node3.ParentNode.Name, node3.InnerText); 
        } 
       } 
      } 
      else 
      { 
       dictionary.Add(node2.Name, node2.InnerText); 
      } 
     } 
    } 
    else 
    { 
     dictionary.Add(node.Name, node.InnerText); 
    } 
} 
+3

查看遞歸。 – jmucchiello 2009-11-10 19:58:25

4

你的問題真的不是很清楚,但我認爲這你想要做什麼:

XmlDocument doc = new XmlDocument(); 
doc.LoadXml(@"<xml> 
<mengde>100</mengde> 
<type>2</type> 
<foo>bar</foo> 
</xml>"); 

Dictionary<string, string> d = new Dictionary<string, string>(); 
foreach (XmlNode n in doc.SelectNodes("/xml/*") 
{ 
    d[n.Name] = n.Value; 
}