Search code examples
c#.netxmlreader

XMLread for XMLwrite


The XMLwrite works fine.

I am trying to XMLread and not getting very far.

    public void WriteXML()
    {
        using (XmlWriter xmlWriter = XmlWriter.Create("logCurves.xml"))
        {
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("log");
            foreach (LogCurve logCurve in Curves)
            {
                xmlWriter.WriteStartElement("logCurveInfo");
                xmlWriter.WriteAttributeString("id", logCurve.Name);
                xmlWriter.WriteElementString("minValue", logCurve.MinValue.ToString());
                xmlWriter.WriteElementString("maxValue", logCurve.MaxValue.ToString());
                xmlWriter.WriteElementString("minIndex", logCurve.MinIndex.ToString());
                xmlWriter.WriteElementString("maxIndex", logCurve.MaxIndex.ToString());
                xmlWriter.WriteElementString("serverCount", logCurve.ServerCount.ToString());
                xmlWriter.WriteElementString("typeLogData", "double");
                xmlWriter.WriteEndElement();
            }
            foreach (LogCurve logCurve in Curves)
            {
                xmlWriter.WriteStartElement("logData");
                xmlWriter.WriteAttributeString("id", logCurve.Name);
                foreach (LogCurveDataPoint logPoint in logCurve.LogPoints)
                {
                    xmlWriter.WriteStartElement("data");
                    xmlWriter.WriteElementString("index", $"{logPoint.Index}");
                    xmlWriter.WriteElementString("value", $"{logPoint.Value}");
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndDocument();
        }
    }
    public void ReadXML()
    {
        MessageBox.Show("Not implemented.");
        Debug.WriteLine("ReadXML");
        using (XmlReader reader = XmlReader.Create("logCurves.xml"))
        {
            while (reader.Read())                   
            {

                if (reader.IsStartElement())
                {
                    Debug.WriteLine($"LocalName            {reader.LocalName}");
                    if (reader.HasAttributes)
                        Debug.WriteLine($"Attribute:           {reader.GetAttribute("id")}");                   
                    Debug.WriteLine($"Value                {reader.ReadString()}");
                }
                else
                {
                    //Debug.WriteLine("not IsStartElement");
                }
            }
        }
        Debug.WriteLine("");
    }

For starters I don't get the first xmlWriter.WriteStartElement("logCurveInfo"); or xmlWriter.WriteAttributeString("id", logCurve.Name); but after the first I do. What is interesting is if I don't get Debug.WriteLine($"Value {reader.ReadString()}"); then I do get the first xmlWriter.WriteStartElement("logCurveInfo"); or xmlWriter.WriteAttributeString("id", logCurve.Name);,

Sample output

<?xml version="1.0" encoding="UTF-8"?>  
-<log>
-<logCurveInfo id="Alpha">    
<minValue>0</minValue>   
<maxValue>750</maxValue>  
<minIndex>100</minIndex>    
<maxIndex>150</maxIndex>   
<serverCount>6</serverCount>    
<typeLogData>double</typeLogData>   
</logCurveInfo>
-<logCurveInfo id="Beta">    
<minValue>0</minValue>    
<maxValue>25</maxValue>    
<minIndex>100</minIndex>    
<maxIndex>150</maxIndex>    
<serverCount>6</serverCount>    
<typeLogData>double</typeLogData>    
</logCurveInfo>

Solution

  • I recommend using xml linq. See code below :

    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace CharConversion
    {
        class Program
        {
            const string IN_FILENAME = @"c:\temp\test.xml";
            const string OUT_FILENAME = @"c:\temp\test1.xml";
            static void Main()
            {
                new Log(IN_FILENAME);
                Log.Write(OUT_FILENAME);
            }
    
        }
        public class Log
        {
            public static List<Log> lists { get; set; }
    
            public string id { get;set;}
            public int minValue { get; set;}
            public int maxValue { get; set;}
            public int minIndex { get;set;}
            public int maxIndex { get; set;}
            public int serverCount { get; set; }
    
            public Log() { }
            public Log(string filename)
            {
                XDocument doc = XDocument.Load(filename);
    
                lists = doc.Descendants("logCurveInfo").Select(x => new Log() {
                    id = (string)x.Attribute("id"),
                    minValue = (int)x.Element("minValue"),
                    maxValue = (int)x.Element("maxValue"),
                    minIndex = (int)x.Element("minIndex"),
                    maxIndex = (int)x.Element("maxIndex"),
                    serverCount = (int)x.Element("serverCount")
                }).ToList();
    
    
            }
    
            public static void Write(string filename)
            {
                string xmlIdent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><log></log>";
                XDocument doc = XDocument.Parse(xmlIdent);
    
                XElement xLog = doc.Root;
    
                foreach (Log log in lists)
                {
                    xLog.Add(new XElement("logCurveInfo", new object[] {
                        new XAttribute("id", log.id),
                        new XElement("minValue", log.minValue),
                        new XElement("maxValue", log.maxValue),
                        new XElement("minIndex", log.minIndex),
                        new XElement("maxIndex", log.maxIndex),
                        new XElement("serverCount", log.serverCount)
                    }));
                }
                doc.Save(filename);
            }
        }
    }