Load XML Data (Key/Value Pairs) into Data Structur

2019-04-02 06:20发布

问题:

I have an XML Data Source which contains a list of key/value pairs. I'm looking for a simple way to load the same data into an array or some other data structure so that I can easily look up the data. I can bind it to a GridView with a couple of clicks but I'm failing to find a straightforward way to load it into something that isn't a UI Control.

My data source looks like:

<SiteMap>
  <Sections>  
    <Section Folder="TradeVolumes" TabIndex="1" />
    <Section Folder="TradeBreaks" TabIndex="2" />
  </Sections>
</SiteMap>

I'm wanting to load key value pairs (Folder, TabIndex)

What is the best way to load the data?

回答1:

Using Linq to XML :

var doc = XDocument.Parse(xmlAsString);
var dict = new Dictionary<string, int>();
foreach (var section in doc.Root.Element("Sections").Elements("Section"))
{
    dict.Add(section.Attribute("Folder").Value, int.Parse(section.Attribute("TabIndex").Value));
}

You get a dictionary, which is basically a collection of key/value pairs



回答2:

Load it into a DataSet with the function

.ReadXml(string path)

with your data you will have a dataset with 2 tables:

Sections 

| section_id |
|------------|
| 0          |

Section

| Folder       | TableIndex | Section_Id | 
|----------------------------------------|
| TradeVolumes | 1          | 0          |
| TradeBreaks  | 2          | 0          |


回答3:

You can do something like this, The code below is based on the on the xml you have included in your questions.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace SimpleTestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlFile = 

            @"<SiteMap>  <Sections><Section Folder=""TradeVolumes"" TabIndex=""1"" />    <Section Folder=""TradeBreaks"" TabIndex=""2"" />  </Sections></SiteMap>";
            XmlDocument currentDocument = new XmlDocument();
            try
            {
                currentDocument.LoadXml(xmlFile);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            string path = "SiteMap/Sections";
            XmlNodeList nodeList = currentDocument.SelectNodes(path);
            IDictionary<string, string> keyValuePairList = new Dictionary<string, string>();
            foreach (XmlNode node in nodeList)
            {
                foreach (XmlNode innerNode in node.ChildNodes)
                {
                    if (innerNode.Attributes != null && innerNode.Attributes.Count == 2)
                    {
                        keyValuePairList.Add(new KeyValuePair<string, string>(innerNode.Attributes[0].Value, innerNode.Attributes[1].Value));
                    }
                }
            }
            foreach (KeyValuePair<string, string> pair in keyValuePairList)
            {
                Console.WriteLine(string.Format("{0} : {1}", pair.Key, pair.Value));
            }
            Console.ReadLine();


        }
    }
}


回答4:

Use XmlSerializer and deserialize it into your own type. Then use it as datasource Quite straightforward example can be found here - http://msdn.microsoft.com/en-us/library/ms950721.aspx