hey all
i am trying to load xml file, add a node and then saving the file
i am trying to do that but sharing violation exception thrown
i have no idea why
i searched a lot but i can't figure out why
this is my code
public static void addNewNodesToXMLFile(string filename, List<T> newNodes)
{
XDocument doc = XDocument.Load(filename);
T el = newNodes[0];
XElement element = ToXElement(el);
doc.Root.Add(element);
try
{
doc.Save(filename);
}
catch (Exception ex)
{
}
}
public static XElement ToXElement(T obj)
{
using (var memoryStream = new MemoryStream())
{
using (TextWriter streamWriter = new StreamWriter(memoryStream))
{
var xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(streamWriter, obj);
var x=XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray()));
return x;
}
}
}
any help please