I have to read an XML file, that has no root element, to extract contained data. The XML has many elements like these:
You xml is simply not well formed which often happens when xml data is merged together. Your xml has multiple tags at root level so use XML reader like below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication4
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(FILENAME,settings);
while (!reader.EOF)
{
try
{
if (reader.Name != "LOG_x0020_ParityRate")
{
reader.ReadToFollowing("LOG_x0020_ParityRate");
}
if (!reader.EOF)
{
XElement parityRate = (XElement)XElement.ReadFrom(reader);
ParityRate newLog = new ParityRate();
ParityRate.logs.Add(newLog);
newLog.date = DateTime.ParseExact((string)parityRate.Element("DATE"), "MM/dd/yyyy - hh:mm", System.Globalization.CultureInfo.InvariantCulture);
newLog.name = (string)parityRate.Element("CHANNELNAME");
newLog.sql = (string)parityRate.Element("SQL");
newLog.hotel = (int)parityRate.Element("ID_HOTEL");
}
}
catch (Exception ex)
{
}
}
}
}
public class ParityRate
{
public static List<ParityRate> logs = new List<ParityRate>();
public DateTime date { get; set; }
public string name { get; set; }
public string sql { get; set; }
public int hotel { get; set; }
}
}
You can try to use XmlParser:
A Roslyn-inspired full-fidelity XML parser with no dependencies and a simple Visual Studio XML language service.
It pars any bad formed xml.
I found a way to solve my problem, I gave up to read it as an XML and I read it as a StreamReader, looking for the text I want to read, so I don't have to fight against the XML format
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());
}
}
}