XML文件读取、保存

前端之家收集整理的这篇文章主要介绍了XML文件读取、保存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
using System.IO;

using System.Xml;


private void GetMsrStep()
{
if (File.Exists(strStepPath))
{

//读取
lstcStrStep.Clear();
XmlReader cXmlReader = XmlReader.Create(strStepPath);
cXmlReader.ReadStartElement("Operation-Steps");
while (XmlNodeType.EndElement != cXmlReader.MoveToContent())
{
XmlNodeType cXNT = cXmlReader.MoveToContent();
List<string> lstTemp = new List<string>(2);
lstTemp.Add(cXmlReader.LocalName);
lstTemp.Add(cXmlReader.ReadElementString());
lstcStrStep.Add(lstTemp);
}


cXmlReader.ReadEndElement();
cXmlReader.Close();
}
else
{

//保存

XmlWriterSettings cXmlWriterSetting = new XmlWriterSettings();
cXmlWriterSetting.ConformanceLevel = ConformanceLevel.Auto;
XmlWriter cXmlWriter = XmlWriter.Create(strStepPath,cXmlWriterSetting);
cXmlWriter.WriteStartDocument();
cXmlWriter.WriteWhitespace("\n");
cXmlWriter.WriteStartElement("Operation-Steps");
cXmlWriter.WriteWhitespace("\n");
foreach (List<string> lstData in lstcStrStep)
{
cXmlWriter.WriteWhitespace(" ");
cXmlWriter.WriteElementString(lstData[0],lstData[1]);
cXmlWriter.WriteWhitespace("\n");
}


cXmlWriter.WriteEndElement();
cXmlWriter.WriteEndDocument();
cXmlWriter.Close();
}
catch
{
}

}

原文链接:https://www.f2er.com/xml/298814.html

猜你在找的XML相关文章