将xml文件写入磁盘:
把要写入内容的空字符做替换,xml.Replace("\0", " ");
string pathG = GPSXMLDir + @"\VehPos-" + SendCarDate + "-" + i + ".xml";
FileStream fs = new FileStream(pathG, FileMode.OpenOrCreate);
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamWriter sw = new StreamWriter(fs);
string xml = vehPos;
xml.Replace("\0", " ");
sw.Write(xml);
sw.Flush();
sw.Close();
fs.Close();
读取xml文件:
XmlDocument doc = new XmlDocument();
doc.Load(file.InputStream);
XmlElement root = doc.DocumentElement;
XmlNodeList tableNodes = root.GetElementsByTagName("Table");
if (tableNodes.Count != 0)
{
foreach (XmlNode node in tableNodes)
{
string Vehicle = ((XmlElement)node).GetElementsByTagName("Vehicle")[0].InnerText;
string GPSTime = ((XmlElement)node).GetElementsByTagName("GPSTime")[0].InnerText;
string Speed = ((XmlElement)node).GetElementsByTagName("Speed")[0].InnerText;
string Odometer = ((XmlElement)node).GetElementsByTagName("Odometer")[0].InnerText;
string Lat = ((XmlElement)node).GetElementsByTagName("Lat")[0].InnerText;
string Lon = ((XmlElement)node).GetElementsByTagName("Lon")[0].InnerText;
string Direction = ((XmlElement)node).GetElementsByTagName("Direction")[0].InnerText;
string Status = ((XmlElement)node).GetElementsByTagName("Status")[0].InnerText;
string RoadName = ((XmlElement)node).GetElementsByTagName("RoadName")[0].InnerText;
VehPositionTest entity = new VehPositionTest
{
Vehicle = Vehicle,
GpsTime = Convert.ToDateTime(GPSTime),
Speed = Convert.ToDecimal(Speed),
OdoMeter = Convert.ToDecimal(Odometer),
Lat = Convert.ToDecimal(Lat),
Lon = Convert.ToDecimal(Lon),
Direction = Convert.ToDecimal(Direction),
Status = Status,
RoadName = RoadName
};
var tuple = Save(entity);
if(tuple)
return true;
}
}
return false;