Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

xdocument save not working

$
0
0

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 :smiley:


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>