2016-02-26 50 views
1

我有一個字符串的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,它包含兩個部分

<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 
}) 

我試圖通過foreach所的XMLNode,然後使用子字符串來獲取值。

+0

請出示第二種方法*正是*你試過了,以[mcve]的形式出了什麼問題。 –

+1

*我嘗試通過foreach在xmlnode上,然後使用substring來獲取值。*那麼,這是一個很好的方法,對你有好處,你有什麼問題嗎? – Gusman

+1

由於您似乎有多個根元素,因此這不是有效的Xml。您需要將每行視爲單獨的Xml文檔,或者將XmlReaderSettings.ConformanceLevel設置爲ConformanceLevel.Fragment;然後相應地後處理它。 – Pawel

回答

0

嘗試正則表達式。我用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和正則表達式

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); 
          } 
         } 
        } 

       } 
      } 
     } 
    } 
}