Hello,
I'm trying to read a .xml or create the .xml if it doesn't exist or it cant find a node/child.
This is what I have so far:
bool XmlErr = false;
if (File.Exists(XMLPath))
{
try
{
XDocument xDoc = XDocument.Load(XMLPath);
}
catch
{
XmlErr = true;
}
if (XmlErr == true)
{
XmlWriterSettings xsettings = new XmlWriterSettings();
xsettings.Indent = true;
XmlWriter xwriter = XmlWriter.Create(XMLPath, xsettings);
xwriter.WriteStartDocument();
xwriter.WriteStartElement("Settings");
xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Flush();
xwriter.Close();
}
}
else
{
XmlWriterSettings xsettings = new XmlWriterSettings();
xsettings.Indent = true;
XmlWriter xwriter = XmlWriter.Create(XMLPath, xsettings);
xwriter.WriteStartDocument();
xwriter.WriteStartElement("Settings");
xwriter.WriteEndElement();
xwriter.WriteEndDocument();
xwriter.Flush();
xwriter.Close();
}
It works but In the Immediate Windows i get: "A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll" which is the "Try/Catch", Its there a better way to do this? thank you