C# 将XML字符串读入列表
我有一个字符串xmlC# 将XML字符串读入列表,c#,xml,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
<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" />
姓名:JON ID:99017代码:111222333类型:ST
NTC:风险:开始:2015年1月9日状态:激活
状态:MS上次取消原因:
A、 TIM(主要)OS 2015年1月9日2016年1月9日
姓名:Tim ID:99017代码:111222333类型:ST
NTC:风险:开始日期:2015年1月9日状态:过期
状态:MS上次取消原因:
A、 TIM(主要)OS 2014年1月9日2015年1月9日
我想把这个字符串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" />
名称:JON ID:99017代码:111222333类型:ST
NTC:风险:开始:2015年1月9日状态:激活
状态:MS上次取消原因:
A、 TIM(主要)OS 2015年1月9日2016年1月9日
及
名称:Tim ID:99017代码:111222333类型:ST
NTC:风险:开始日期:2015年1月9日状态:过期
状态:MS上次取消原因:
A、 TIM(主要)OS 2014年1月9日2015年1月9日
我想把元素按如下方式放入列表中
var subjects= new List<subject>();
subjects.Add(new subject()
{
Name = JON,
State = MS
})
var主题=新列表();
主题。添加(新主题()
{
Name=JON,
状态=毫秒
})
我尝试在xmlnode上使用foreach,然后使用子字符串获取值。尝试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);
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列表主题=新列表();
字符串xml=
“\n”+
“\n”+
“名称:JON ID:99017代码:111222333类型:ST\n”+
“NTC:风险:开始:2015年1月9日状态:活动\n”+
“状态:MS上次取消原因:\n”+
“A,TIM(主要)OS 09/01/2015 09/01/2016\n”+
“\n”+
“名称:Tim ID:99017代码:111222333类型:ST\n”+
“NTC:风险:开始日期:2015年1月9日状态:过期\n”+
“状态:MS上次取消原因:\n”+
“A,TIM(主要)操作系统2014年1月9日2015年1月9日\n”+
“\n”;
字符串模式1=“0)
{
MatchCollection matches=Regex.matches(innertext,pattern2);
foreach(匹配中的匹配2)
{
string name=match2.Groups[“name”].Value.Trim();
string value=match2.Groups[“value”].value.Trim();
主题。添加(名称、值);
}
}
}
}
}
}
}
这里是使用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);
}
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列表主题=新列表();
字符串xml=
“\n”+
“\n”+
“名称:JON ID:99017代码:111222333类型:ST\n”+
“NTC:风险:开始:2015年1月9日状态:活动\n”+
“状态:MS上次取消原因:\n”+
“A,TIM(主要)OS 09/01/2015 09/01/2016\n”+
“\n”+
“名称:Tim ID:99017代码:111222333类型:ST\n”+
“NTC:风险:开始日期:2015年1月9日状态:过期\n”+
“状态:MS上次取消原因:\n”+
“A,TIM(主要)操作系统2014年1月9日2015年1月9日\n”+
“\n”;
XmlReaderSettings设置=新建XmlReaderSettings();
settings.ConformanceLevel=ConformanceLevel.Fragment;
StringReader=新的StringReader(xml);
XmlReader=XmlReader.Create(读取器,设置);
字符串模式=@“((?'name'[^:]*):\s(?'value'[\w0-9/\]*))”;
字典主题=null;
而(!xReader.EOF)
{
如果(xReader.Name!=“消息”)
{
xReader.ReadToFollowing(“消息”);
}
if(!xReader.EOF)
{
XElement消息=(XElement)XElement.ReadFrom(xReader);
字符串代码=(字符串)message.Attribute(“代码”);
如果(代码==“D1”)
{
主题=新词典();
主题。添加(主题);
}
其他的
{
字符串innertext=(字符串)消息;
如果(innertext.Length>0)
{
MatchCollection matches=Regex.matches(innertext,pattern);
foreach(匹配中的匹配2)
{
string name=match2.Groups[“name”].Value.Trim();
string value=match2.Groups[“value”].value.Trim();
主题。添加(名称、值);
}
}
}
}
}
}
}
}
试试正则表达式。我用D1表示
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);
}
}
}
}
}
}
}
}