将XML字符串读入列表

时间:2016-02-26 23:14:08

标签: c# xml

我有一个字符串xml

<message code="L1" />
<message code="D1" />
<message code="A1">NAME: JON              ID: 99017   CODE: 111222333    TYPE: ST</message>
<message code="A2">NTC:           RISK:               START: 09/01/2015     STATUS: ACTIVE</message>
<message code="CD">STATE: MS     LAST CANCEL REASON:</message>
<message code="A4">A, TIM                   (PRIMARY)      OS      09/01/2015    09/01/2016</message>
<message code="D1" />
<message code="A1">NAME: Tim              ID: 99017   CODE: 111222333    TYPE: ST</message>
<message code="A2">NTC:           RISK:               START: 09/01/2015     STATUS: EXPIRED</message>
<message code="CD">STATE: MS     LAST CANCEL REASON:</message>
<message code="A4">A, TIM                   (PRIMARY)      OS      09/01/2014    09/01/2015</message>               
<message code="D1" />

我想将这个字符串xml读入列表中。如果你看到这个xml,它包含2个部分

<message code="A1">NAME: JON              ID: 99017   CODE: 111222333    TYPE: ST</message>
<message code="A2">NTC:           RISK:               START: 09/01/2015     STATUS: ACTIVE</message>
<message code="CD">STATE: MS     LAST CANCEL REASON:</message>
<message code="A4">A, TIM                   (PRIMARY)      OS      09/01/2015    09/01/2016</message>
<message code="D1" />

<message code="A1">NAME: Tim              ID: 99017   CODE: 111222333    TYPE: ST</message>
<message code="A2">NTC:           RISK:               START: 09/01/2015     STATUS: EXPIRED</message>
<message code="CD">STATE: MS     LAST CANCEL REASON:</message>
<message code="A4">A, TIM                   (PRIMARY)      OS      09/01/2014    09/01/2015</message>               
<message code="D1" />

我希望将元素列入

列表
var subjects= new List<subject>();
subjects.Add(new subject()
{
  Name = JON,
  State = MS
 })

我在forex上尝试了xmlnode,然后使用substring来获取值。

1 个答案:

答案 0 :(得分:0)

试试Regex。我使用D1来启动每个主题,但您可能需要忽略D1并使用A1

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Dictionary<string, string>> subjects = new List<Dictionary<string, string>>();
            string xml =
                "<message code=\"L1\" />\n" +
                "<message code=\"D1\" />\n" +
                "<message code=\"A1\">NAME: JON              ID: 99017   CODE: 111222333    TYPE: ST</message>\n" +
                "<message code=\"A2\">NTC:           RISK:               START: 09/01/2015     STATUS: ACTIVE</message>\n" +
                "<message code=\"CD\">STATE: MS     LAST CANCEL REASON:</message>\n" +
                "<message code=\"A4\">A, TIM                   (PRIMARY)      OS      09/01/2015    09/01/2016</message>\n" +
                "<message code=\"D1\" />\n" +
                "<message code=\"A1\">NAME: Tim              ID: 99017   CODE: 111222333    TYPE: ST</message>\n" +
                "<message code=\"A2\">NTC:           RISK:               START: 09/01/2015     STATUS: EXPIRED</message>\n" +
                "<message code=\"CD\">STATE: MS     LAST CANCEL REASON:</message>\n" +
                "<message code=\"A4\">A, TIM                   (PRIMARY)      OS      09/01/2014    09/01/2015</message>\n" +
                "<message code=\"D1\" />\n";

            string pattern1 = "<message code=\"(?'code'[^\"]*)\"(>(?'innertext'[^<]*))?";
            string pattern2 = @"((?'name'[^:]*):\s?(?'value'[\w0-9/\<\>]*)?)";
            StringReader reader = new StringReader(xml);
            string inputLine = "";

            Dictionary<string, string> subject = null;
            while((inputLine = reader.ReadLine()) != null)
            {
                Match match1 = Regex.Match(inputLine, pattern1);
                string code = match1.Groups["code"].Value;
                string innertext = match1.Groups["innertext"].Value;

                if (code == "D1")
                {
                    subject = new Dictionary<string, string>();
                    subjects.Add(subject);
                }
                else
                {
                    if (innertext.Length > 0)
                    {
                        MatchCollection matches = Regex.Matches(innertext, pattern2);
                        foreach (Match match2 in matches)
                        {
                            string name = match2.Groups["name"].Value.Trim();
                            string value = match2.Groups["value"].Value.Trim();
                            subject.Add(name, value);
                        }
                    }
                }
            }

        }
    }
}

这是使用XML和Regex的第二种方法

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Dictionary<string, string>> subjects = new List<Dictionary<string, string>>();
            string xml =
                "<message code=\"L1\" />\n" +
                "<message code=\"D1\" />\n" +
                "<message code=\"A1\">NAME: JON              ID: 99017   CODE: 111222333    TYPE: ST</message>\n" +
                "<message code=\"A2\">NTC:           RISK:               START: 09/01/2015     STATUS: ACTIVE</message>\n" +
                "<message code=\"CD\">STATE: MS     LAST CANCEL REASON:</message>\n" +
                "<message code=\"A4\">A, TIM                   (PRIMARY)      OS      09/01/2015    09/01/2016</message>\n" +
                "<message code=\"D1\" />\n" +
                "<message code=\"A1\">NAME: Tim              ID: 99017   CODE: 111222333    TYPE: ST</message>\n" +
                "<message code=\"A2\">NTC:           RISK:               START: 09/01/2015     STATUS: EXPIRED</message>\n" +
                "<message code=\"CD\">STATE: MS     LAST CANCEL REASON:</message>\n" +
                "<message code=\"A4\">A, TIM                   (PRIMARY)      OS      09/01/2014    09/01/2015</message>\n" +
                "<message code=\"D1\" />\n";

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            StringReader reader = new StringReader(xml);
            XmlReader xReader = XmlReader.Create(reader, settings);

            string pattern = @"((?'name'[^:]*):\s?(?'value'[\w0-9/\<\>]*)?)";

            Dictionary<string, string> subject = null;
            while (!xReader.EOF)
            {
                if (xReader.Name != "message")
                {
                    xReader.ReadToFollowing("message");
                }
                if (!xReader.EOF)
                {
                    XElement message = (XElement)XElement.ReadFrom(xReader);
                    string code = (string)message.Attribute("code");
                    if (code == "D1")
                    {
                        subject = new Dictionary<string, string>();
                        subjects.Add(subject);
                    }
                    else
                    {
                        string innertext = (string)message;
                        if (innertext.Length > 0)
                        {
                            MatchCollection matches = Regex.Matches(innertext, pattern);
                            foreach (Match match2 in matches)
                            {
                                string name = match2.Groups["name"].Value.Trim();
                                string value = match2.Groups["value"].Value.Trim();
                                subject.Add(name, value);
                            }
                        }
                    }

                }
            }
        }
    }
}