XMLread for XMLwrite

时间:2018-04-27 15:15:46

标签: c# .net xmlreader

XMLwrite工作正常。

我正在尝试XMLread而不是走得太远。

    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("");
    }

对于初学者,我不会得到第一个xmlWriter.WriteStartElement("logCurveInfo");xmlWriter.WriteAttributeString("id", logCurve.Name);,但是在我做第一个之后。有趣的是,如果我没有Debug.WriteLine($"Value {reader.ReadString()}");,那么我会得到第一个xmlWriter.WriteStartElement("logCurveInfo");xmlWriter.WriteAttributeString("id", logCurve.Name);

示例输出

<?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>

1 个答案:

答案 0 :(得分:1)

我建议使用xml linq。请参阅以下代码:

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);
        }
    }
}