Hi,
It is much easier to create an object and serialise and deserialise xml than manually reading and writing them.
Try the code below:
using System;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
namespace Test
{
[Serializable]
[XmlRoot("RootElement")]
public class RootElement
{
[XmlElement]
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public class Test
{
public void setXmlValues()
{
RootElement testOut = new RootElement {
Id = 1,
Name = "Customer Name",
Address = "Customer Address"
};
xml_serialise(testOut);
RootElement testIn = xml_deserialise();
int id = testIn.Id;
string Name = testIn.Name;
string address = testIn.Address;
}
private void xml_serialise(RootElement test)
{
XmlSerializer ser = new XmlSerializer(typeof(RootElement));
using (TextWriter writer = new StreamWriter("test.xml"))
{
ser.Serialize(writer, test);
}
}
private RootElement xml_deserialise()
{
XmlSerializer ser = new XmlSerializer(typeof(RootElement));
RootElement test;
using (TextReader writer = new StreamReader("test.xml"))
{
test = (RootElement)ser.Deserialize(writer);
}
return test;
}
}
}
Regards
Jegan