I'm trying to read a given file saved in xml, but I'm getting the error "Object reference not set to an instance of an object."
Edit: I cannot use any kind of serialization for this.
I'm trying to read a given file saved in xml, but I'm getting the error "Object reference not set to an instance of an object."
Edit: I cannot use any kind of serialization for this.
Easiest approach you can have for such case is using XmlSerializer. That is not the only approach you can do with .net, as there are XmlReader, XmlTextReader and XDocument to help you with that but XmlSerializer allow you to easily convert data structure to xml and back. Here is an example:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace TestXmlSerializer
{
class Program
{
static void Main(string[] args)
{
var g = new Group
{
Name="g2",
Keys = new[] {
new Key { Username="a" },
new Key { Password="b" }
}
};
Group g2;
var xs = new XmlSerializer(typeof(Group));
var s = string.Empty;
using (var tw = new StringWriter()) {
using (var xw = XmlWriter.Create(tw))
xs.Serialize(xw, g);
s = tw.ToString();
}
Console.WriteLine(s);
using (var ms = new StringReader(s))
{
using (var xw = XmlReader.Create(ms))
g2 = xs.Deserialize(xw) as Group;
}
Console.WriteLine(g2.Name);
}
}
[Serializable]
public class Key
{
[XmlAttribute]
public string Title;
[XmlAttribute]
public string Username;
[XmlAttribute]
public string Password;
[XmlAttribute]
public string Url;
[XmlAttribute]
public string Notes;
}
[Serializable]
public class Group
{
[XmlAttribute]
public string Name;
[XmlElement]
public Key[] Keys;
}
}