我必须读取一个没有根元素的XML文件来提取包含的数据。 XML有许多这样的元素:
<DocumentElement>
<LOG_x0020_ParityRate>
<DATE>12/09/2017 - 00:00</DATE>
<CHANNELNAME>ParityRate</CHANNELNAME>
<SQL>update THROOMDISP set ID_HOTEL = '104', ID_ROOM = '920', NUM = '3', MYDATA = '20171006' where id_hotel =104 and id_room ='920' and MYDATA ='20171006'</SQL>
<ID_HOTEL>104</ID_HOTEL>
<TYPEREQUEST>updateTHROOMDISP(OK)</TYPEREQUEST>
</LOG_x0020_ParityRate>
</DocumentElement><DocumentElement>
<LOG_x0020_ParityRate>
<DATE>12/09/2017 - 00:00</DATE>
<CHANNELNAME>ParityRate</CHANNELNAME>
<SQL>update THROOMDISP set ID_HOTEL = '105', ID_ROOM = '923', NUM = '1', MYDATA = '20171006' where id_hotel =105 and id_room ='923' and MYDATA ='20171006'</SQL>
<ID_HOTEL>105</ID_HOTEL>
<TYPEREQUEST>updateTHROOMDISP(OK)</TYPEREQUEST>
</LOG_x0020_ParityRate>
</DocumentElement><DocumentElement>
<LOG_x0020_ParityRate>
<DATE>12/09/2017 - 00:00</DATE>
<CHANNELNAME>ParityRate</CHANNELNAME>
<SQL>update THROOMDISP set ID_HOTEL = '104', ID_ROOM = '920', NUM = '3', MYDATA = '20171007' where id_hotel =104 and id_room ='920' and MYDATA ='20171007'</SQL>
<ID_HOTEL>104</ID_HOTEL>
<TYPEREQUEST>updateTHROOMDISP(OK)</TYPEREQUEST>
</LOG_x0020_ParityRate>
</DocumentElement><DocumentElement>
我尝试将其作为字符串读取,手动添加开始和结束标记,并像XDocument一样解析它,但它也有一些格式错误的标记,如这些
</DocumentElement>
<TYPEREQUEST>updateTHROOMPRICE(OK)</TYPEREQUEST>
这些标记与任何开始标记都不匹配,当我在结果字符串上调用XDocument.Parse
时,我有例外。该文件有数百万行,因此我无法逐行读取,或者迭代将持续数小时。如何摆脱所有这些格式错误的标签并解析文档?
答案 0 :(得分:2)
当xml数据合并在一起时,xml根本就没有很好地形成。您的xml在根级别有多个标记,因此请使用如下所示的XML阅读器:
select customerid , monthinvoice from yourtable
where yearinvoice in (2016, 2017) and monthinvoice = 'July'
group by customerid , monthinvoice
having count(*) = 2
答案 1 :(得分:1)
我找到了一种方法来解决我的问题,我放弃将其作为XML阅读,我将其作为StreamReader阅读,寻找我想要阅读的文本,所以我不必反对XML格式
using (StreamReader strReader = File.OpenText(path))
{
while (!strReader.EndOfStream)
{
string line = strReader.ReadLine();
if (line.Contains("<LOG_x0020_ParityRate>")) {
line = strReader.ReadLine();
string data_ = getTagText(line);
string channelName_ = getTagText( strReader.ReadLine());
string sql_ = getTagText( strReader.ReadLine());
string idHotel_ = getTagText(strReader.ReadLine());
string type_ = getTagText(strReader.ReadLine());
}
}
}
答案 2 :(得分:0)