J
jodleren
Hi
As you might now, I am new to all this, therefore I ask - there must
be a better way than this?
I have created an XML file for holding settings. Saving data to it
looks as follows. But is is big, and there probably is a better way to
check items etc:
My XML
<root>
<ComPort>COM5</ComPort>
<HostAdr>99</HostAdr>
</root>
XmlDocument doc = new XmlDocument();
string s =
Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".xml";
if (File.Exists(s))
{
doc.Load(s);
}
if (ReadSettings(ref doc, "HostAdr", "N/A") == "N/
A") *****
{
XmlElement root;
if (doc.DocumentElement.Name == "root")
{
root = doc.DocumentElement;
}
else
{
root = doc.CreateElement("root");
doc.AppendChild(root);
}
XmlElement elem =
doc.CreateElement("HostAdr");
root.AppendChild(elem);
XmlText text =
doc.CreateTextNode(v.ToString());
doc.DocumentElement.LastChild.AppendChild(text);
}
else
{
for (int i = 0; i <
doc.DocumentElement.ChildNodes.Count; i++)
{
if
(doc.DocumentElement.ChildNodes.Item(i).Name == "HostAdr")
{
doc.DocumentElement.ChildNodes.Item(i).InnerText = v.ToString();
break;
}
}
}
doc.Save(s);
**** this is :
private string ReadSettings(ref XmlDocument doc, string item,
string defaultvalue)
{
try
{
for (int i = 0; i <
doc.DocumentElement.ChildNodes.Count; i++)
{
if (doc.DocumentElement.ChildNodes.Item(i).Name ==
item)
{
return
doc.DocumentElement.ChildNodes.Item(i).InnerText;
}
}
}
catch
{
}
return defaultvalue;
}
As you might now, I am new to all this, therefore I ask - there must
be a better way than this?
I have created an XML file for holding settings. Saving data to it
looks as follows. But is is big, and there probably is a better way to
check items etc:
My XML
<root>
<ComPort>COM5</ComPort>
<HostAdr>99</HostAdr>
</root>
XmlDocument doc = new XmlDocument();
string s =
Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".xml";
if (File.Exists(s))
{
doc.Load(s);
}
if (ReadSettings(ref doc, "HostAdr", "N/A") == "N/
A") *****
{
XmlElement root;
if (doc.DocumentElement.Name == "root")
{
root = doc.DocumentElement;
}
else
{
root = doc.CreateElement("root");
doc.AppendChild(root);
}
XmlElement elem =
doc.CreateElement("HostAdr");
root.AppendChild(elem);
XmlText text =
doc.CreateTextNode(v.ToString());
doc.DocumentElement.LastChild.AppendChild(text);
}
else
{
for (int i = 0; i <
doc.DocumentElement.ChildNodes.Count; i++)
{
if
(doc.DocumentElement.ChildNodes.Item(i).Name == "HostAdr")
{
doc.DocumentElement.ChildNodes.Item(i).InnerText = v.ToString();
break;
}
}
}
doc.Save(s);
**** this is :
private string ReadSettings(ref XmlDocument doc, string item,
string defaultvalue)
{
try
{
for (int i = 0; i <
doc.DocumentElement.ChildNodes.Count; i++)
{
if (doc.DocumentElement.ChildNodes.Item(i).Name ==
item)
{
return
doc.DocumentElement.ChildNodes.Item(i).InnerText;
}
}
}
catch
{
}
return defaultvalue;
}