Hudlommen Hudlommen - 1 month ago 28
C# Question

How to read and write from file in C# UWA (Universal Windows App)

Okay, this might end up being a bit of a duplicate of my previous question.
But i have now for 5-6 hours tried to figure out how to read/write from and XML, txt, whatever file. (just give me something! approach)

So what have i gotten so far. Well i need to open a fileStream.
I have no basic idea of how to do that, so my knowledge is very fractured, and i am quite certain that it is here my problem lies.

FileStream fs = new FileStream(@"C:\XML\test.txt", FileMode.Create, FileAccess.Write);


So i found out about this code, seems to work, red lines.
Then in the end of all that i am told to put in flush and close, like this:

FileStream fs = new FileStream(@"C:\XML\test.txt", FileMode.Create,

...

fs.Flush();
fs.Close();


Now, already i hit a snag, because fs.Close(); is not even on the list? I just gets a red line if i try to hardcode it, no luck..

Can someone (please take the time) to make me understand how to do this with UWA. For some reason it seems like there is a different approach in Windows 10 apps, and i have a VERY hard time finding anything that shows me how to do it right. All the tutorials and SOF forum input are about older versions (non-UWA).

When i do this in a console application i all works as expected..

My end goal is to be able to read and write to an XML file in this kind of fashion:

XDocument doc = XDocument.Load(input);
XElement person = doc.Element("Person");
person.Add(new XElement("Employee",
new XElement("Name", "David"),
new XElement("Dept", "Chef")));
doc.Save(output);


}

A dude in my previous question told me to have a filestream, but i simple can not make that work in UWA

Answer

Okay, the (simple) solution is to put the xml-file in the PROJECTFOLDER/bin/x86/debug/appX and then write the data to a list this way:

public class dataRaw
    {
        public string data { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
    }

    //You can call this class with x = collectionGenerator.getList() (it returns a list<T>)
    public class collectionGenerator
    {
       public static List<dataRaw> getList()
        {
            //This is the xml file in the folder
            var doc = XDocument.Load("Data.xml"); 

            //This parse the XML and adds in to the list "dataList"
            var dataList = doc.Root
                .Descendants("Person")
                .Select(node => new dataRaw
                {
                    //data, firstName and lastName are in app variables from dataRaw put into listData.
                    //Number, FirstName and LastName are the nodes in the XML file.
                    data = node.Element("Number").Value,
                    firstName = node.Element("FirstName").Value,
                    lastName = node.Element("LastName").Value,
                })
            .ToList();
            return dataList;
        }
    }