How can I load the following XML using LINQ-to-XML into a dictionary?

不问归期 提交于 2019-12-21 05:34:09

问题


How can I load the following formatted XML document:

<Settings>
    <MimeTypes>
        <MimeType Type="application/mac-binhex40" Extensions=".hqx"/>
        <MimeType Type="application/msword" Extensions=".doc;.docx"/>
        <MimeType Type="application/pdf" Extensions=".pdf"/>
        <MimeType Type="application/vnd.ms-excel" Extensions=".xla;.xlc;.xlm;.xls;.xlt;.xlw;.xlsx"/>
    </MimeTypes> 
</Settings>

Into a dictionary where the key is an individual extension, and the value is the mimetype.

So, for this line:

<MimeType Type="application/vnd.ms-excel" Extensions=".xla;.xlc;.xlm;.xls;.xlt;.xlw;.xlsx"/>

I would have the following key-value entries:

Key: ".xla" Value: "application/vnd.ms-excel"
Key: ".xlc" Value: "application/vnd.ms-excel"
Key: ".xlm" Value: "application/vnd.ms-excel"
Key: ".xls" Value: "application/vnd.ms-excel"
Key: ".xlt" Value: "application/vnd.ms-excel"

I'm relatively new to the LINQ-To-XML business.

I know that I should load in the document into an XElement like:

 XElement settingsDoc = XElement.Load("Settings.xml");

However, how to do I select all "MimeType" entries?


回答1:


Something like:

 var dictionary = (from element in settingsDoc.Descendants("MimeType")
                   from extension in element.Attribute("Extensions")
                                         .Value.Split(';')
                   select new { Type = element.Attribute("Type").Value,
                                Extension = extension })
                   .ToDictionary(x => x.Extension,
                                 x => x.Type);



回答2:


This is my solution.

 XElement el = XElement.Parse(txt);
            var mimeTypes = el.Element("MimeTypes").Elements("MimeType");
            var transFormed = mimeTypes.Select(x =>
                    new
                    {
                        Type = x.Attribute("Type").Value,
                        Extensions = x.Attribute("Extensions").Value.Split(';')
                    }
                       );
            Dictionary<string, string> mimeDict = new Dictionary<string, string>();
            foreach (var mimeType in transFormed)
            {
                foreach (string ext in mimeType.Extensions)
                {
                    if (mimeDict.ContainsKey(ext))
                        mimeDict[ext] = mimeType.Type;
                    else
                        mimeDict.Add(ext, mimeType.Type);
                }
            }

Okay, after looking at Jonh's code...here is my 2nd solution :)

XElement el = XElement.Parse(txt);
var mimeTypes = el.Element("MimeTypes").Elements("MimeType");
var dictionary = mimeTypes.SelectMany(x => x.Attribute("Extensions").Value.Split(';').Select(
                                               ext => new
                                                {
                                                    Key = ext,
                                                    Value = x.Attribute("Type").Value
                                                 }
                                              )
                                     ).ToDictionary( x => x.Key, y => y.Value);



回答3:


Here is my contribution.

        Dictionary<string, string> dic = new Dictionary<string,string>();
        foreach (XElement element in settingsDoc.Descendants("MimeType"))
        {
            string val = element.Attribute("Type").Value;
            foreach (string str in element.Attribute("Extensions").Value.Split(';'))
                if (!dic.ContainsKey(str)) dic.Add(str, val);
        }


来源:https://stackoverflow.com/questions/2015773/how-can-i-load-the-following-xml-using-linq-to-xml-into-a-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!