如何在其中一个元素未在中间关闭的情况下读取XML文件.(C#) [英] How to Read XML file in that one of elements are not closed in the middle.(c#)

查看:73
本文介绍了如何在其中一个元素未在中间关闭的情况下读取XML文件.(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行SSIS打包时,我正在读取SSIS日志文件.在此过程中,日志文件未完全格式化为xml结构.包执行完成后,我们可以使用XPathDocument方法读取日志文件.在执行程序包时,我们没有以正确的XML文档格式获取日志文件.因此,我们无法以XmlDocument的形式读取此文件,但可以使用StreamReader读取此文件.如果使用StreamReader,则无法找到节点和元素.
这是StreamReader的代码:

I am reading SSIS log file when SSIS packaging is executing. In this process Log file is not formatted completely in xml structure. After completion of package execution we can read the log file, by using XPathDocument method . While execution of the package we are not getting log file in correct XML document format. So we can’t read this file as a XmlDocument , but we can read this file using StreamReader . If we use StreamReader then we are unable to find nodes and elements.
This is code for StreamReader :

FileStream logFileStream = new FileStream("C\\Log.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);            StreamReader logFileReader = new StreamReader(logFileStream);
            while (!logFileReader.EndOfStream)
            {
                string line = logFileReader.ReadLine();
                Console.WriteLine(line);
                Console.ReadLine();
            }


如果我使用logFileReader.ReadLine(),则XML文档如下所示,我得到了一行,但是我没有得到
的值 事件:OnPreExecute
开始时间:17.1.12 PM 12:44:02,
结束时间:17.1.12 PM 12:44:02


XML document is like this below if I use logFileReader.ReadLine() I get line but I can’t get value of
Event : OnPreExecute
starttime: 17.1.12 PM 12:44:02,
endtime : 17.1.12 PM 12:44:02

<record> 
  <event>OnPreExecute</event> 
 <starttime>17.1.12 PM 12:44:02</starttime> 
  <endtime>17.1.12 PM 12:44:02</endtime>   
  </record>


在这里缺少一些标签


Missing some tag here

sourceid>{61AA27B7-6876-4549-84CE-080A5B0A4E16} 
  <starttime>17.1.12 PM 12:44:02</starttime> 
  <endtime>17.1.12 PM 12:44:02</endtime>


您能在这个问题上建议我吗?
我必须读取事件,开始时间,结束时间值.在此XML文件中.


Can you please suggest me on this issue.
I have to read event ,start time,end time values. In this XML file.

推荐答案

尝试使用称为substring的代码

Try Using Code called substring

string a = "code";
for(int i = 0;i < a.substring;i++)
{
    if(a.substring(i,1)=="o")
    {
        MessageBox.Show("I found a letter o");
    }
}



a.substring(i,1)
| AAAAAAAAA || |
| AAAAAAAAA || |-您想要阅读多少个字母
| AAAAAAAAA |
| AAAAAAAAA | ---应从其开始读取的位置
|
| --------------您要读取的字符串



a.substring(i,1)
|AAAAAAAAA| |
|AAAAAAAAA| |- how many letters you want to read
|AAAAAAAAA|
|AAAAAAAAA|--- position from which should reading start
|
|-------------- string you want to read


如果格式不正确,则它不是XML文件.您必须确定要查找的模式,然后将其作为字符串处理.如果树结构对您很重要,那么您将必须制定一些规则来确定未关闭的标签何时隐式关闭.

如果文件只是不完整(即,直到文件末尾是正确的,但某些标签未关闭),则可以编写一个非常简单的预解析器,计算出未关闭的标签是什么,结束标记到文件末尾,并将其提供给XML解析器.
If it''s not correctly formatted, then it''s not an XML file. You''ll have to work out what the patterns you want to look for are, and deal with it as a string. If the tree structure is important to you, then you''ll have to work out some rules for when unclosed tags get implicitly closed.

If the file is simply incomplete (i.e. it is correct up to the end of file but some tags are unclosed), you can write a very simple pre-parser that works out what the unclosed tags are, adds the closing tags to the end of the file, and gives it to an XML parser.


这篇关于如何在其中一个元素未在中间关闭的情况下读取XML文件.(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆