2009-11-10 222 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; 
}