0

可以使用 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

谢谢

4

3 回答 3

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

它将返回一个匿名类型对象的列表。

于 2013-09-10T08:21:47.350 回答
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
于 2013-09-10T08:26:24.717 回答
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);
}
于 2013-09-10T08:25:14.957 回答