重复写入不同的 XML 文件

Repeated writing in different XMLfiles

我需要在不同的 XML 文件中写入 GPS 数据。这些文件需要具有以下格式:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx>
<metadata>
<time>YYYY-MM-DDTHH:MM:SSZ</time>
</metadata>
<trk>
<name>YYYY-MM-DDTHH:MM:SSZ</name>
<trkseg>
<trkpt lat="12.1795" lon="12.3456">
<ele>-46.97</ele>
<time>YYYY-MM-DDTHH:MM:SSZ</time>
</trkpt>
// more trkpt
</trkseg>
</trk>
</gpx>

我试着用 XMLwriter 来做。首先它构建部分直到 trkpt.That 工作。包含 XmlWriterSettings 是因为错误告诉我使用 ConformanceLevel "Auto" 或 "fregmented" ,但这并没有解决我的问题。

XmlWriterSettings setting = new XmlWriterSettings();
setting.ConformanceLevel = ConformanceLevel.Auto;
setting.Indent = true;

DateTime localDate = DateTime.Now;
xmlWriter[0] = XmlWriter.Create("testbase.xml", setting);      
for (int i = 1; i < (numOfXMLWriter); i++)      
{
    xmlWriter[i] = XmlWriter.Create(test[i].Text, setting);      
}
int tmp = 0;
foreach (XmlWriter writer in xmlWriter)
{
writer.WriteStartDocument(true);
writer.WriteStartElement("gpx");
writer.WriteStartElement("metadata");
writer.WriteStartElement("time");
writer.WriteString(localDate.Year + "-" + localDate.Month + "-" + localDate.Day + "T" + localDate.Hour + ":" + localDate.Minute + ":" + localDate.Second);
writer.WriteEndElement();       //time
writer.WriteEndElement();       //metadata
                                //trk + name
writer.WriteStartElement("trkseg");

}

稍后将接收到的 GPS 数据写入单独的 trkpt。

xmlWriter[id].WriteStartElement("trkpt");
xmlWriter[id].WriteAttributeString("lat", splitData[4]);
xmlWriter[id].WriteAttributeString("lon", splitData[6]);
xmlWriter[id].WriteStartAttribute("ele");
xmlWriter[id].WriteString("0");  
xmlWriter[id].WriteEndElement();        //</ele>
xmlWriter[id].WriteStartElement("time");
xmlWriter[id].WriteString(splitData[10][4] + splitData[10][5] + "-" + splitData[10][2] + splitData[10][3] + "-" + splitData[10][0] + splitData[10][1] + "T" + splitData[2][0] + splitData[2][1] + ":" + splitData[2][2] + splitData[2][3] + ":" + splitData[2][4] + splitData[2][5] + "Z");
xmlWriter[id].WriteEndElement();        //</time>
xmlWriter[id].WriteEndElement();        //</trkpt>

错误说:

InvalidOperationException: "Token StartElement in state EndRootElement would result in an invalid XML document”

我认为这是因为 XMLwriter 自动关闭了所有节点,后来我尝试在根级别添加另一个节点。是否有可能阻止作者自行结束文档? 谢谢你的帮助! 乔纳斯

使用 xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            string header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?><gpx></gpx>";

            XDocument doc = XDocument.Parse(header);
            XElement gpx = doc.Root;

            gpx.Add(new object[] {
                new XElement("metadata"), new XElement("time", DateTime.Now.ToString("yyyy-MM-ddthh:mm:ssz")),
                new XElement("trk",  new object[] {
                    new XElement("name", DateTime.Now.ToString("yyyy-MM-ddthh:mm:ssz")),
                    new XElement("trksseg", new object[] {
                        new XElement("trkpt", new object[] {
                            new XAttribute("lat", 12.1795),
                            new XAttribute("lon", 12.3456),
                            new XElement("ele", -46.97),
                            new XElement("time",DateTime.Now.ToString("yyyy-MM-ddthh:mm:ssz")),
                        })
                    })
                })
            });

        }
    }
}