0

I am develop win8 metro app and I need to load xml file to present on app. But I got a problem after loading xml files located in folders.

I know I can use xmlreader to read a xml file in app itself, but I don't know how to load a file in "My Document" or any other folder out of app. So I google for it and I found I can using await XmlDocument.LoadFromFileAsync(file) to load but I don't know how to make xmlreader to read the file I loaded in xmldocument.

Or there is a way xmlreader can load xml files out of app?

I've tried to put the file path for xmlreader but it always respond "File Not Found" and the route is start from my develop directory but not from the path I set.

Can any one help me ?

below is how I deal with xmlreader, so I hope the file from xmldocument could be handled by this way:

            XmlReader reader = XmlReader.Create(@"Data/question/" + file_name);
            while (reader.Read())
            {
                //                subject.Text += "start-node:\n";
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Element = reader.Name;

                        //                        subject.Text += String.Format("Element: {0}\n", reader.Name);
                        if (reader.HasAttributes)
                        {
                            //                            subject.Text += String.Format("Attributes of " + reader.Name + "\n");
                            while (reader.MoveToNextAttribute())
                            {
                                switch (Element)
                                {
                                    case "SUBJECT":
                                        switch (reader.Name)
                                        {
                                            case "TYPE":
                                                type = int.Parse(reader.Value);
                                                if (start_type == 999)
                                                {
                                                    start_type = type;
                                                    Save_para("Start_Type", start_type);
                                                }
                                                last_sn += sn;
                                                sn = 0;
                                                break;
                                            case "QUANTITY":
                                                quantity = int.Parse(reader.Value);
                                                question[type] = new string[quantity + 1][];

                                            ....
Bency
  • 21
  • 1
  • 1
  • 4
  • 1
    A Sample code describing your situation will help. Hint : Always paste a code sample to give more clarity to your problem. – Taher Oct 31 '12 at 09:45
  • Any reason you really want to use `XmlReader`? It's a pretty ugly API compared with others, particularly LINQ to XML. Is the XML file involved enormous? – Jon Skeet Oct 31 '12 at 09:46
  • The XML files won't have the same attributes or the same subnode so i can't appoint the node's name in code – Bency Oct 31 '12 at 09:59
  • @Bency But you already use strings like `"QUANTITY"`, `"SUBJECT"` – L.B Oct 31 '12 at 10:01
  • @L.B hmm.. I don't know how to move to next node or next element like while(reader.read()) – Bency Oct 31 '12 at 10:08

2 Answers2

2

I found a solution from here, I use a StringReader to read my xmldocument and loaded by xmlreader.

            StorageFolder folder = await KnownFolders.DocumentsLibrary.CreateFolderAsync("documents", CreationCollisionOption.OpenIfExists);
            StorageFile file = await folder.GetFileAsync(file_name);
            XmlDocument reade = await XmlDocument.LoadFromFileAsync(file);
            //XmlReader reader = XmlReader.Create(@"Data/question/" + file_name);
            XmlReader reader = XmlReader.Create(new StringReader(reade.GetXml()));
Community
  • 1
  • 1
Bency
  • 21
  • 1
  • 1
  • 4
1

From the description of your problem I think you need to do something like this in order to select a file from the device

 //function to read an XML file form the local device
 private async void SelectXMLButton_Click(object sender, RoutedEventArgs e)
    {
        var filePicker = new Windows.Storage.Pickers.FileOpenPicker();
        //addign a a filter so only XML files will show up.
        filePicker.FileTypeFilter.Add(".xml");

        var selectedXMlFile = await filePicker.PickSingleFileAsync();
        // now your reader should open the file
        XmlReader reader = XmlReader.Create(selectedXMlFile.Path);

    }

Please note I haven't tested the function , but it should work.

Updated : // to say ... access a file from my documents you can do :

XmlReader reader = XmlReader.Create(@"%UserProfile%\My Documents\file.xml");

And you will also have to allow access to Documents folder from the Capabilities tab in the Appmanifest file.

I think this would help you more.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nikola Sivkov
  • 2,812
  • 3
  • 37
  • 63
  • thank for answering but I don't use file picker in this app, I want app can load file automatically. Thanks again! – Bency Oct 31 '12 at 10:57
  • Could not find a part of the path 'C:\Users\Bency\Dropbox\student_client\Live_Test\bin\Debug\AppX\%UserProfile%\My Documents\documents20120113_002.xml'. Im sorry this is not working – Bency Oct 31 '12 at 11:26
  • try this: `var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var path = Path.Combine(folder, "file.xml");` – Roland Nov 20 '12 at 20:45