解析在C#中使用字典<字符串,字符串>

问题描述:

我是新来编程,并一直在努力,以解析文件。我最初试图解析它以一定的方式,但最终没有正常工作。我想分析的字典&LT以下线;字符串,字符串>。

I am new to programming and have been trying hard to parse a file. I, initially was trying to parse it in a certain way, but that didn't end up working correctly. I want to parse the following line in a Dictionary< string,string> .

网络卡(S):7 NIC(S)已设置

Network Card(s): 7 NIC(s) Installed.

                       [01]: Broadcom 
                             Connection Name: Local Area Connection
                             DHCP Enabled:    No
                             IP address(es)
                             [01]: abc.de.xyz.
                       [02]: Broadcom 
                             Connection Name: eth1
                             Status:          Media disconnected
                       [03]: Broadcom 
                             Connection Name: eth0
                             Status:          Media disconnected
                       [04]: Broadcom 
                             Connection Name: eth3
                             Status:          Media disconnected
                       [05]: Mellanox 
                             Connection Name: Local Area Connection 5
                             Status:          Hardware not present
                       [06]: Mellanox 
                             Connection Name: Local Area Connection 6
                             Status:          Media disconnected
                       [07]: Mellanox 
                             Connection Name: Local Area Connection 7
                             DHCP Enabled:    No
                             IP address(es)
                             [01]: mno.pqr.stu.vwx

我要[01]博通为重点,以字典和连接名称:本地连接启用DHCP:没有IP地址(ES)[01]:abc.de.xyz作为值等了其他六。谢谢您的帮助。真的AP preciate它。如何着手做这将是伟大的,因为我已经疯了念叨分割字符串,并试图找出如何让字典存储值任何帮助。

I want [01] Broadcom as the key to the dictionary and Connection Name: Local Area Connection DHCP Enabled: No IP address(es) [01]: abc.de.xyz as the value and so on for the other six. Thanks for the help. Really appreciate it. Any help on how to go about doing it will be great since I have gone crazy reading about splitting strings and trying to figure out how to get the dictionary to store the value.

下面是不使用正则表达式,如果你不希望采取这种路线的解决方案。这code已通过测试。

Here is a solution that does not use regex if you don't want to take that route. This code has been tested.

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

namespace NicParser
{
    public class NicFileParser
    {
        private readonly string _file;
        private readonly Dictionary<string, string> _nics;

        public NicFileParser(string file)
        {
            _file = file;
            _nics = new Dictionary<string, string>();
        }

        public void Parse()
        {
            var key = string.Empty;
            var value = new StringBuilder();

            try
            {
                using (var rdr = new StreamReader(_file))
                {
                    var firstTime = true;

                    while (rdr.Peek() > 0)
                    {
                        var line = rdr.ReadLine().Trim();

                        if (IsKey(line))
                        {
                            // Once a key is hit, add the previous 
                            // key and values (except the first time).
                            if (!firstTime)
                            {
                                _nics.Add(key, value.ToString());
                            }
                            else
                            {
                                firstTime = false;
                            }

                            // Assign the key, and clear the previous values.
                            key = line;
                            value.Length = 0;
                        }
                        else
                        {
                            // Add to the values for this nic card.
                            value.AppendLine(line);
                        }
                    }

                    // Final line of the file has been read. 
                    // Add the last nic card.
                    _nics.Add(key, value.ToString());
                }
            }
            catch (Exception ex)
            {
                // Handle your exceptions however you like...
            }
        }

        private static bool IsKey(string line)
        {
            return (!String.IsNullOrEmpty(line)
                 && line.StartsWith("[") 
                 && !line.Contains("."));
        }

        // Use this to access the NIC information.
        public Dictionary<string, string> Cards
        {
            get { return _nics; }
        }
    }
}