เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

Last post 9 Apr 2011 16:03 by aaron. 8 replies.
Page 1 of 2 (9 items) 1 2 Next >
Sort Posts: Previous Next
  • 5 Apr 2011 23:52

    • aaron
    • Top 25 Contributor
    • Joined on 25 Jun 2010
    • Posts 25

    เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    คือว่าในกรณีที่มีไฟล์ข้อมูลอยู่แล้ว เช่น เป็นแบบ .xml อะครับ  ไม่ทราบว่าจะเรียกใช้แบบ isolated storage สำหรับ WP7 อย่างไรหรอครับ

    ขอบคุณล่วงหน้าครับ

  • 6 Apr 2011 9:29 In reply to

    Re: เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    ลองใช้  XmlSerializer ดูนะครับ

    อย่างโค้ดข้างล่าง เราจะอ่าน XML มาใส่ไว้ใน List Person เพื่อนำไป binding ครับ แบบนี้ครับ

    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
    using(IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Open))
    {
    XmlSerializer serializer = new XmlSerializer(typeof(List));
    List data = (List)serializer.Deserialize(stream);
    this.listBox.ItemsSource = data;
    }
    }
  • 6 Apr 2011 12:58 In reply to

    • aaron
    • Top 25 Contributor
    • Joined on 25 Jun 2010
    • Posts 25

    Re: เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    ขอบคุณสำหรับคำแนะนำครับ  Big Smile

    ผมลองทำดูแล้วครับ ดังนี้
    private void searchBttn_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    using (IsolatedStorageFile isoStoreFile = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream isoFileStream = isoStoreFile.OpenFile("vocab.xml", FileMode.Open))
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(List<Vocab>));
                            List<Vocab> data = (List<Vocab>)serializer.Deserialize(isoFileStream);
                            outputListbox.ItemsSource = data;
                        }
                    }
                }
                catch
                {
                      MessageBox.Show("Cannot connect vocab.xml");
                }
            }

    ซึ่งพอลองรันดู โปรแกรมมันไม่เข้าไปทำงานใน try loop ครับ มันเข้าไปทำใน catch loop เลย  ไม่ทราบว่าเป็นเพราะอะไรครับ

  • 6 Apr 2011 14:32 In reply to

    • aaron
    • Top 25 Contributor
    • Joined on 25 Jun 2010
    • Posts 25

    Re: เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    ผมลองเอา try{} catch{} ออกครับ  พอลองรันอีกทีมันจะขึ้น error ตรงโค้ด

    IsolatedStorageFileStream isoFileStream = isoStoreFile.OpenFile("vocab.xml", FileMode.Open)

    ว่า IsolatedStorageException was unhandled : "Operation not permitted on IsolatedStorageFileStream." ครับ

    ไม่ทราบว่า error นี้หมายความว่าอย่างไรหรอครับ
    ขอบคณล่วงหน้าครับ  ^^

  • 6 Apr 2011 22:34 In reply to

    Re: เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    เข้าใจว่าตอนรันครั้งแรก มันอ่านไฟล์ vocab.xml ไม่เจอครับ

    ยังไงลองเปลี่ยนมาใช้เป็น FileMode.OpenOrCreate แทน FileMode.Open ดูก่อนนะครับ ว่าได้ไหม
  • 6 Apr 2011 23:06 In reply to

    • aaron
    • Top 25 Contributor
    • Joined on 25 Jun 2010
    • Posts 25

    Re: เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    ขอบคุณมากครับ  ^^

    ตอนนี้ code ปัจจุบันเป็นแบบนี้ครับ
    try
                {
                    using (IsolatedStorageFile isoStoreFile = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream isoFileStream = isoStoreFile.OpenFile("vocab.xml", FileMode.OpenOrCreate))
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(List<Vocab>));
                            List<Vocab> data = (List<Vocab>)serializer.Deserialize(isoFileStream);
                            outputListbox.ItemsSource = data;
                        }
                    }
                }
                catch(IsolatedStorageException isoException)
                {
                    MessageBox.Show(isoException.Message);
                }

     พอรันคราวนี้เกิด error อันนี้ขึ้นมาครับ "There is an error in XML document (0, 0)."    งงเลยครับ  T_T

    ขอบคุณครับ

  • 7 Apr 2011 9:29 In reply to

    Re: เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    error นี่้จะเกิดตอน deserialize ดังนั้นก็น่าจะเป็นที่ XML ครับ เช่นว่า format มันไม่ถูกต้อง หรือไม่มี root element ของ xml อะครับ

    อ้างอิงจากกระทู้ด้านล่างนี้ครับ

    http://forums.asp.net/t/1381821.aspx

    http://social.msdn.microsoft.com/Forums/en/xmlandnetfx/thread/f5a5ff54-3743-400c-b14d-92dae95f3605
  • 7 Apr 2011 9:58 In reply to

    • aaron
    • Top 25 Contributor
    • Joined on 25 Jun 2010
    • Posts 25

    Re: เรียกใช้ .xml มาเป็นแบบ isolated storage ทำอย่างไรครับ

    ไฟล์ที่ผมใช้ เอามาจากของ Lexitron อะครับ  ซึ่งเคยทำวิธีคล้ายๆกัน คือ เอาข้อมูลจาก xml ลงไปใน SQL server CE ของ WM6.5 app
    โดย xml มีข้อมูลคร่าวๆ ดังนี้ครับ

    <?xml version="1.0" standalone="yes"?>
    <Dictionary>
    <Doc>
    <esearch>a</esearch>
    <eentry>a</eentry>
    <tentry>หนึ่ง (คำนำหน้าคำนามเพื่อแสดงว่าคำนามนั้นๆ ไม่ชี้เฉพาะ)</tentry>
    <ecat>DET</ecat>
    <ethai></ethai>
    <esyn></esyn>
    <id>0</id>
    </Doc>
    <Doc>
    <esearch>A</esearch>
    <eentry>A</eentry>
    <tentry>อักษรตัวแรกในภาษาอังกฤษ</tentry>
    <ecat>N</ecat>
    <ethai></ethai>
    <esyn></esyn>
    <id>1</id>
    </Doc>
    :
    :
    :

    ซึ่งเท่าที่ดูก็น่าจะมี root อยู่นะครับ หรือว่าผมเข้าใจผิด    ก็เลยงงว่าเกิดผิดพลาดอย่างไร

    ขอบคุณล่วงหน้าครับ  ^^

Page 1 of 2 (9 items) 1 2 Next >