C# 将 XML 文档转换为字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1710193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting an XML-document to a dictionary
提问by Phoexo
I do not need to edit any XML-file or anything, this is only for reading and parsing.
我不需要编辑任何 XML 文件或任何东西,这仅用于读取和解析。
I want to be able to handle the XML-document as a dictionary, like: username = doc["username"];
, but I can't find out how to "convert" the document. I've also encountered the problem with duplicate key-names, but that could be easlily avoided by appending each value with 1, 2 etc; making it easy to for-loop through too.
我希望能够将 XML 文档作为字典处理,例如: username = doc["username"];
,但我不知道如何“转换”文档。我也遇到过重复键名的问题,但是可以通过为每个值附加 1、2 等来轻松避免这种情况;也很容易进行循环。
Is this possible? To treat the (parsed) XML-document as a dictionary?
这可能吗?将(解析的)XML 文档视为字典?
Answer to Mehrdad:
It varies from time to time, it depends on the request from the user. If the user requests x
, then it will be:
对 Mehrdad 的回答:它不时变化,这取决于用户的要求。如果用户请求x
,那么它将是:
<xml>
<test>foo</test>
<bar>123</bar>
<username>foobar</username>
</xml>
But if he requests y
, it will be like
但如果他提出要求y
,就会像
<xml>
<ammount>1000</ammount>
<mail>...@...</mail>
<username>foobar</username>
</xml>
The best would be if this:
最好是这样:
<xml>
<mengde>100</mengde>
<type>3</type>
<mail>foo</mail>
<crypt>bar</crypt>
<username>bar</username>
</xml>"
Could be parsed and then accessed as doc["mengde"]
etc.
可以被解析,然后作为doc["mengde"]
等访问。
采纳答案by mdm20
You could use linq to xml to do what you want (if I understand what you want)
你可以使用 linq to 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);
}
回答by Tarik
XML Data
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>
Conversion Code
转换代码
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);
}
This question was asked before here and so you can find the all answers in this link :
这个问题在这里之前被问过,所以你可以在这个链接中找到所有答案:
convert xml to sorted dictionary
Hope it helps.
希望能帮助到你。
回答by nikmd23
This isn't exactly what you are looking for, but may be of interest: http://blogs.msdn.com/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx
这不是您正在寻找的,但可能会感兴趣:http: //blogs.msdn.com/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the -expandoobject.aspx
回答by Robert Rossney
Your question's really not very clear, but I think this does what you want:
你的问题真的不是很清楚,但我认为这符合你的要求:
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;
}