questions/50079147/xmlreader-behaves-different-with-line-breaks

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using System.Globalization;

//https://stackoverflow.com/questions/50079147/xmlreader-behaves-different-with-line-breaks

	public class LogData
	{
		public string ID { get; }
		public List<LogPoint> LogPoints { get; } = new List<LogPoint>();
		public LogData (string id)
		{
			ID = id;
		}
	}
	public class LogPoint
	{
		public int Index { get; }
		public double Value { get; }
		public LogPoint ( int index, double value)
		{
			Index = index;
			Value = value;
		}
	}

    public class TestClass
    {
		public static void Test()
		{
			var xmlFile = "singleVersusMultLine.xml";
			var xml = GetXml();
			
			try
			{
				File.WriteAllText(xmlFile, xml);
				Test(xmlFile);
			}
			finally
			{
				File.Delete(xmlFile);
			}
		}
		
        public static void Test(string xmlFile)
        {
			List<LogData> logDatas = GetLogDatasFromFile(xmlFile);
			Console.WriteLine("Main");
			Console.WriteLine("logData");
			foreach (LogData logData in logDatas)
			{
				Console.WriteLine($"    logData.ID {logData.ID}");
				foreach(LogPoint logPoint in logData.LogPoints)
				{
					Console.WriteLine($"        logData.Index {logPoint.Index}  logData.Value {logPoint.Value}");
				}
			}
			Console.WriteLine("end");			
        }
		
		static string GetXml()
		{
			var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<log>
   <logData id=""Alpha"">
      <data><index>100</index><value>150</value></data>
      <data><index>110</index><value>750</value></data>
      <data><index>120</index><value>750</value></data>
      <data><index>130</index><value>150</value></data>
      <data><index>140</index><value>0</value></data>
      <data><index>150</index><value>222</value></data>
   </logData>
   <logData id=""Bravo"">
      <data>
         <index>100</index>
         <value>25</value>
      </data>
      <data>
         <index>110</index>
         <value>11</value>
      </data>
      <data>
         <index>120</index>
         <value>1</value>
      </data>
      <data>
         <index>130</index>
         <value>25</value></data>
      <data>
         <index>140</index>
         <value>0</value>
      </data>
      <data>
         <index>150</index>
         <value>1</value>
      </data>
   </logData>
</log>";
			return xml;
		}
		
		
		public static List<LogData> GetLogDatasFromFile(string xmlFile)
		{
			List<LogData> logDatas = new List<LogData>();
		
			using (XmlReader reader = XmlReader.Create(xmlFile))
			{
				// move to next "logData"
				while (reader.ReadToFollowing("logData", ""))
				{
					var logData = new LogData(reader.GetAttribute("id"));
					using (var logDataReader = reader.ReadSubtree())
					{
						// inside "logData" subtree, move to next "data"
						while (logDataReader.ReadToFollowing("data", ""))
						{
							// move to index
							logDataReader.ReadToFollowing("index", "");
							// read index
							var index = XmlConvert.ToInt32(logDataReader.ReadElementContentAsString());
							// move to value
							logDataReader.ReadToFollowingOrCurrent("value", "");
							// read value
							var value = XmlConvert.ToDouble(logDataReader.ReadElementContentAsString());
							logData.LogPoints.Add(new LogPoint(index, value));
						}
					}
					logDatas.Add(logData);
				}
			}
			return logDatas;
		}		
    }

	public static class XmlReaderExtensions
	{
		public static bool ReadToFollowingOrCurrent(this XmlReader reader, string localName, string namespaceURI)
		{
			if (reader == null)
				throw new ArgumentNullException(nameof(reader));
			if (reader.NodeType == XmlNodeType.Element && reader.LocalName == localName && reader.NamespaceURI == namespaceURI)
				return true;
			return reader.ReadToFollowing(localName, namespaceURI);
		}
	}
				
    public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
						
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Uncaught exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;