可以使用linq解析XML格式吗?它有多个属性,我需要解析LogRecord节点中的每个记录。
<?xml version="1.0"?>
<SignedLogs>
<Header IP="::000.000.000.000" Port="0000" ET="0021" CS="568 47" FormatVersion="01.01.01" RefName="CE42xx" />
<Logs NumOfRec="1" CreationDate="2013/09/05 07:42">
<LogRecord DeliveryDate="Mon Jul 29 02:40:32 2013 UTC" AK="00" SN="" RC2="xxxxx2" NC="0000000000000000" C="00000000" NUID="ascdsadas" ViD="0000000000000000"/>
<LogRecord DeliveryDate="Tue Jul 30 02:40:32 2013 UTC" AK="10" SN="" RC2="xxdsdx2" NC="0000000000000000" C="00000000" NUID="789" ViD="0000900000000000"/>
</Logs>
</SignedLogs>
LogRecord中每行所需的数据: DeliveryDate,AK,SN,RC2,NC,C,NUID,ViD
感谢
答案 0 :(得分:1)
var xDoc = XDocument.Load("source.txt");
var records = xDoc.Root.Element("Logs")
.Elements("LogRecord")
.Select(x => new
{
DeliveryDate = (string)x.Attribute("DeliveryDate"),
AK = (string)x.Attribute("AK"),
SN = (string)x.Attribute("SN"),
RC2 = (string)x.Attribute("RC2"),
NC = (string)x.Attribute("NC"),
C = (string)x.Attribute("C"),
NUID = (string)x.Attribute("NUID"),
ViD = (string)x.Attribute("ViD"),
}).ToList();
它将返回一个匿名类型对象列表。
答案 1 :(得分:0)
你可以试试这个:
string xml = @"<?xml version='1.0'?>
<SignedLogs>
<Header IP='::000.000.000.000' Port='0000' ET='0021' CS='568 47' FormatVersion='01.01.01' RefName='CE42xx' />
<Logs NumOfRec='1' CreationDate='2013/09/05 07:42'>
<LogRecord DeliveryDate='Mon Jul 29 02:40:32 2013 UTC' AK='00' SN='' RC2='xxxxx2' NC='0000000000000000' C='00000000' NUID='ascdsadas' ViD='0000000000000000'/>
<LogRecord DeliveryDate='Tue Jul 30 02:40:32 2013 UTC' AK='10' SN='' RC2='xxdsdx2' NC='0000000000000000' C='00000000' NUID='789' ViD='0000900000000000'/>
</Logs>
</SignedLogs>";
XDocument doc = XDocument.Parse(xml);
IEnumerable<XElement> logRecordNodeList = doc.Descendants("LogRecord"); //get the LogRecord node.
var logs = (from r in logRecordNodeList
select new
{
DeliveryDate = r.Attribute("DeliveryDate").Value,
AK = r.Attribute("AK").Value,
SN = r.Attribute("SN").Value,
RC2 = r.Attribute("RC2").Value,
NC = r.Attribute("NC").Value,
C = r.Attribute("C").Value,
NUID = r.Attribute("NUID").Value,
ViD = r.Attribute("ViD").Value
}).ToList();
foreach (var log in logs)
{
Console.Write("Delivery Date: ");
Console.WriteLine(log.DeliveryDate);
Console.Write("AK: ");
Console.WriteLine(log.AK);
Console.Write("SN: ");
Console.WriteLine(log.SN);
Console.Write("RC2: ");
Console.WriteLine(log.RC2);
Console.Write("NC: ");
Console.WriteLine(log.NC);
Console.Write("C: ");
Console.WriteLine(log.C);
Console.Write("NUID: ");
Console.WriteLine(log.NUID);
Console.Write("ViD: ");
Console.WriteLine(log.ViD);
}
答案 2 :(得分:0)
当然,您可以解析此XML。
您没有说出您使用的语言,所以让我借此机会宣传VB.Net的xml文字:
Dim xml = <?xml version="1.0"?>
<SignedLogs>
<Header IP="::000.000.000.000" Port="0000" ET="0021" CS="568 47" FormatVersion="01.01.01" RefName="CE42xx" />
<Logs NumOfRec="1" CreationDate="2013/09/05 07:42">
<LogRecord DeliveryDate="Mon Jul 29 02:40:32 2013 UTC" AK="00" SN="" RC2="xxxxx2" NC="0000000000000000" C="00000000" NUID="ascdsadas" ViD="0000000000000000"/>
<LogRecord DeliveryDate="Tue Jul 30 02:40:32 2013 UTC" AK="10" SN="" RC2="xxdsdx2" NC="0000000000000000" C="00000000" NUID="789" ViD="0000900000000000"/>
</Logs>
</SignedLogs>
For Each logrecord in xml...<LogRecord>
Console.WriteLine(String.Format("{0}: {1}", logrecord.@AK, logrecord.@RC2))
Next
<强>输出:强>
00: xxxxx2
10: xxdsdx2