解析XML文件中的两个连续记录

时间:2013-04-09 23:05:05

标签: c# xml linq

我有一个XML文件:

<root type="service">
  <Msg Date="03/23/2013 12:00:04 AM">Request'HANDSHAKE'</Msg>
  <Msg Date="03/23/2013 12:00:04 AM">Response'RSHANDSHAKE'</Msg>
  <Msg Date="03/23/2013 12:03:04 AM">Request'HANDSHAKE'</Msg>
  <Msg Date="03/23/2013 12:03:04 AM">Response'RSHANDSHAKE'</Msg>
  <Msg Date="03/23/2013 01:34:30 PM">Request 'IQ~bbabb3ff2-...DLE~VNECTRECVBDHANDLE'</Msg>
  <Msg Date="03/23/2013 01:34:30 PM">Response SIQ~7a23da12...RDHANDLE=O000000000014'</Msg>
</root>

我必须解析文件并查看每个请求后面是否都有响应。如果没有响应,则表示存在错误。我怎样才能在C#中执行此操作,最好是LINQ?

3 个答案:

答案 0 :(得分:3)

您应该使用XML解析器,如LINQ to XML。有了它,您的代码可能如下所示:

XDocument doc = XDocument.Load("file.xml");
bool isError = (from e1 in doc.Root.Elements()
                where e1.Value.StartsWith("Request")
                let e2 = e1.ElementsAfterSelf().FirstOrDefault()
                where e2 == null || !e2.Value.StartsWith("Response")
                select e1).Any();

本文采用了您的要求:对于每个Request元素,它会检查以下元素是否存在且是Response元素。

如果您有其他要求,例如Response元素本身也无效,您必须相应地修改代码。

答案 1 :(得分:-1)

这样的东西会起作用,我只是在Visual Studio中测试过,肯定有更优雅的方法可以使用XPATH过滤器/选择器来完成部分工作...

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

namespace ParseXML
{
    class Program
    {
        static void Main(string[] args)
        {
            String xmlString =
        @"<root type=""service"">
                <Msg Date=""03/23/2013 12:00:04 AM"">Request'HANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 12:00:04 AM"">Response'RSHANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 12:03:04 AM"">Request'HANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 12:03:04 AM"">Response'RSHANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 01:34:30 PM"">Request 'IQ~bbabb3ff2-...DLE~VNECTRECVBDHANDLE'</Msg>
                <Msg Date=""03/23/2013 01:34:30 PM"">Response SIQ~7a23da12...RDHANDLE=O000000000014'</Msg>
            </root>";

            // Create an XmlReader
            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                while (reader.ReadToFollowing("Msg"))
                {
                    string request = reader.ReadElementContentAsString();

                    if(reader.ReadToFollowing("Msg"))
                    {
                        string response = reader.ReadElementContentAsString();

                        if (request.Contains("Request") && response.Contains("Response"))
                        {
                            // Request/Response nodes identified...
                        }
                    }
                }
            }
        }
    }
}

答案 2 :(得分:-1)

这是一个适用于有效案例和错误的解决方案,并且还提供了确切的数量:

    String xmlString = @"<root type=""service"">
        <Msg Date=""03/23/2013 12:00:04 AM"">Request'HANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 12:00:04 AM"">Response'RSHANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 12:03:04 AM"">Request'HANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 12:03:04 AM"">Response'RSHANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 01:34:30 PM"">Request 'IQ~bbabb3ff2-...DLE~VNECTRECVBDHANDLE'</Msg>
        <Msg Date=""03/23/2013 01:34:30 PM"">Response SIQ~7a23da12...RDHANDLE=O000000000014'</Msg>
    </root>";

    String[] data = xmlString.Split(new string[] { "/Msg"},  StringSplitOptions.None);

    int errors = 0;
    Boolean secondLine = false;
    for (int i = 0; i+1 < data.Length; i ++ )
    {
        if (secondLine)
        {
            if (!data[i].Contains("Response"))
            {
                errors += 1;
            }
            secondLine = false;
        }
        else
        {
            if (!data[i].Contains("Request"))
            {
                errors += 1;
            }
            else
            {
                secondLine = true;
            }
        }
    }
    if (secondLine) { errors += 1; }

    System.Windows.Forms.MessageBox.Show("Errors: " + errors.ToString());