PDA

View Full Version : Need C# xml ready/write example - Stummped!


Timb320
18th September 2008, 12:34 AM
OK I am writing my first Mobile 6 App, based on compact framework 2, obviously finding some differences from Framwork vs compact when dealing with reading and writting simple values to xml;

Here is what I want to do: write two textbox values to xml as follows:

<settings>
<hostip>192.168.0.14</hostip>
<hostport>8011</hostport>
</settings>

So I got the above accomplished using the following code:
--------------------
string hstip = "";
string hstport = "";

hstip = this.HostIP.Text.ToString();
hstport = this.HostPort.Text.ToString();
XmlWriterSettings xml_settings = new XmlWriterSettings();
xml_settings.Indent = true;

xml_settings.OmitXmlDeclaration = false;
xml_settings.Encoding = Encoding.UTF8;
xml_settings.ConformanceLevel = ConformanceLevel.Auto;

using (XmlWriter xml = XmlTextWriter.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.WriteStartElement("settings");
//xml.WriteStartAttribute("hostip", "");
xml.WriteElementString("hostip", hstip);
//xml.WriteString(hstip);
//xml.WriteEndAttribute();

xml.WriteElementString("hostport", hstport);
//xml.WriteStartAttribute("hostport", "");
//xml.WriteString(hstport);
//xml.WriteEndAttribute();

xml.WriteEndElement();
//ds.WriteXml(xml);
xml.Close();
}
-----------end of write code------------

Now the part I am stumped on as I have tried all varietions to get the data back into the textboxes, I either get and exception or blank values in the textboxes.

Here is the code:
------------------
private void SrvSettings_Load(object sender, EventArgs e)
{
XmlReaderSettings xml_settings = new XmlReaderSettings();
xml_settings.IgnoreComments = true;
xml_settings.IgnoreProcessingInstructions = true;
xml_settings.IgnoreWhitespace = true;
xml_settings.CloseInput = true;
try
{
using (XmlReader xml = XmlTextReader.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{

xml.ReadStartElement("settings");

xml.Read();
xml.ReadElementString(,,)
//xml.ReadToFollowing("hostip");
this.HostIP.Text = xml.GetAttribute("hostip","");
//this.HostIP.Text = xml.GetAttribute("hostip");
//xml.ReadToFollowing("hostport");
this.HostPort.Text = xml.GetAttribute("hostport","");

// Close it out----------------
xml.ReadEndElement();

xml.Close();
//ds.ReadXml(xml);
// }
}

}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(
ex.ToString(), "Error Loading IP settings");

}
}

------------end of read code

obviously I get an exception with this:

"System.xml.xmlexception:
"Text" is an invalid XmlNoteType. Line 2, posistion 11 at
System.xml.xmlReader.ReadEndElement()":confused:

-----

I suspect I need to use ReadElementString, but I have tried and no workie...

Somebody have an example/class for both reading and writing per my requirements above, I would really appreciate the help.. :)

replay37
18th September 2008, 12:48 AM
Try using the XmlDocument instead. It's alot easier to use for simple xml reading/writing.

Timb320
18th September 2008, 01:51 AM
Well Partially working now :) - I get the Hostport populated but for some reason hostip is blank even though it appears it falls through it in the case statement.. wierd..

DataSet ds = new DataSet();
XmlReaderSettings xml_settings = new XmlReaderSettings();
xml_settings.IgnoreComments = true;
xml_settings.IgnoreProcessingInstructions = true;
xml_settings.IgnoreWhitespace = true;
xml_settings.CloseInput = true;

try
{
using (XmlReader xml = XmlTextReader.Create(GetApplicationDirectory() + @"\settings.xml", xml_settings))
{
xml.MoveToContent();
xml.ReadStartElement("settings");
while (xml.Read())
{
//xml.Read();
switch (xml.Name)
{
case "hostip":
HostIP.Text = xml.ReadString(); <--I get Blank text
break;
case "hostport":
HostPort.Text = xml.ReadString(); <--POpulated OK
break;
default:
break;
}
// Close it out----------------
//xml.ReadEndElement();
}
xml.Close();
//ds.ReadXml(xml);
// }
}

}

Timb320
18th September 2008, 02:38 AM
Solved, Had to remove the line: xml.ReadStartElement("settings");

Thanks..

xplode
18th September 2008, 02:53 AM
Just out of curiosity, what will your app do ?

Timb320
18th September 2008, 03:45 AM
I'm writing a WPF Windows App that monitors Server Services, IPs, Websites for up or down status; buiding a client server component to it, so the end goal will be to use the mobile app to connect to the host and download status on said servers being monitored and populate them in a listview whith thier up or done status.

Simple app but still in the works.

xplode
18th September 2008, 04:41 AM
if you can make it customizable (to set up parametres, adreses) it wil be useful for many website administrators

a cool little app :)