从Windows Mobile应用程序加载XML文件时出错 [英] Error in Loading XML File from windows Mobile appliction

查看:56
本文介绍了从Windows Mobile应用程序加载XML文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#在Visual Studio紧凑框架中使用Windows Mobile 5.0 Pocket PC电话模拟器开发Windows Mobile应用程序.

我的应用程序正在将xml文档内容加载到我的应用程序中.加载时,在将xml文档加载到我的应用程序时出现错误:
" doc.Load("//Student.xml");"中的错误作为IOException
我的XML文件是:

I am developing windows mobile application using windows mobile 5.0 pocket pc phone emulator in visual studio compact frame work using c#.

My application is loading the xml document content into my application. While I am loading, I get an error in loading the xml document into my application:
Error in ""doc.Load("//Student.xml");"" as IOException
my XML File is:

<?xml version="1.0" encoding="utf-8"?>
<Students>
<!-- student1 details -->
    <StudentDetails>
        <ID>1</ID>
        <FirstName>Anand</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student2 details -->
    <StudentDetails>
        <ID>2</ID>
        <FirstName>bala</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student3 details -->
    <StudentDetails>
        <ID>3</ID>
        <FirstName>Banu</FirstName>
        <Age>20</Age>
<Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student4 details -->
    <StudentDetails>
        <ID>4</ID>
        <FirstName>chitra</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student5 details -->
   <StudentDetails>
        <ID>5</ID>
        <FirstName>saranya</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student6 details -->
    <StudentDetails>
        <ID>6</ID>
        <FirstName>prasanth</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student7 details -->
    <StudentDetails>
        <ID>7</ID>
        <FirstName>suresh</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
    </StudentDetails>

<!-- student8 details -->
    <StudentDetails>
        <ID>8</ID>
        <FirstName>nandhini</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student9 details -->
    <StudentDetails>
        <ID>9</ID>
        <FirstName>keerthi</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>

<!-- student10 details -->
   <StudentDetails>
        <ID>10</ID>
        <FirstName>vinu</FirstName>
        <Age>20</Age>
        <Mark1>90</Mark1>
        <Mark2>90</Mark2>
        <Mark3>90</Mark3>
        <Mark4>90</Mark4>
        <Mark5>90</Mark5>
        <Total>450</Total>
        <Avg>90</Avg>
</StudentDetails>
</Students>


以下是我的代码


below is my code

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Reflection;

namespace Axml1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\New Folder\\student.xml");
            XmlNodeList list = doc.SelectNodes("/Students/StudentDetails");
            foreach (XmlNode xn in list)
            {
                string ID = xn["ID"].InnerText;
                lbID.Items.Add(ID);
            }
        }

        private void lbID_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\New Folder\\student.xml");
            string rollno = lbID.Text;
            XmlNodeList xnList = doc.SelectNodes("/Students/StudentDetails");

            foreach (XmlNode xn1 in xnList)
            {
                string rollNumber = xn1["ID"].InnerText;
                string Name = xn1["FirstName"].InnerText;
                string Age = xn1["Age"].InnerText;
                string Mark1 = xn1["Mark1"].InnerText;
                string Mark2 = xn1["Mark2"].InnerText;
                string Mark3 = xn1["Mark3"].InnerText;
                string Mark4 = xn1["Mark4"].InnerText;
                string Mark5 = xn1["Mark5"].InnerText;
                string Total = xn1["Total"].InnerText;
                string Avg = xn1["Avg"].InnerText;

                if (rollno == rollNumber)
                {
                    txtName.Text = Name;
                    txtAge.Text = Age;
                    txtMark1.Text = Mark1;
                    txtMark2.Text = Mark2;
                    txtMark3.Text = Mark3;
                    txtMark4.Text = Mark4;
                    txtMark5.Text = Mark5;
                    txtTotal.Text = Total;
                    txtAvg.Text = Avg;
                    break;
                }
            }
        }
    }
}

推荐答案

"//Student.xml" is not a valid URL or local file name for PC.

Is it supposed to be a file at root directory? For PC it is invalid, because the absolute path is started from the drive letter. As a relative path it is invalid.

You should not use hard-coded absolute or relative path anyway. If the path is relative, it is relative to working directory, which is not known (the same application can be run from different working directories). This addressing of the path still can be used if you want to have different files depending on where you start the application.

The absolute path name is simply not portable at all (if hard-coded).

Normally, the data file name should be calculated. If the file is not modifiable by the non-privileged user, it can be placed is some location relative to System.Reflection.Assembly.GetEntryAssembly().Location, otherwise it should be placed in some user dependent "special" locations, such as System.Environment.SpecialFolder.LocalApplicationData, see System.Environment.GetFolderPath(SpecialFolder).

—SA
.
"//Student.xml" is not a valid URL or local file name for PC.

Is it supposed to be a file at root directory? For PC it is invalid, because the absolute path is started from the drive letter. As a relative path it is invalid.

You should not use hard-coded absolute or relative path anyway. If the path is relative, it is relative to working directory, which is not known (the same application can be run from different working directories). This addressing of the path still can be used if you want to have different files depending on where you start the application.

The absolute path name is simply not portable at all (if hard-coded).

Normally, the data file name should be calculated. If the file is not modifiable by the non-privileged user, it can be placed is some location relative to System.Reflection.Assembly.GetEntryAssembly().Location, otherwise it should be placed in some user dependent "special" locations, such as System.Environment.SpecialFolder.LocalApplicationData, see System.Environment.GetFolderPath(SpecialFolder).

—SA
.


这篇关于从Windows Mobile应用程序加载XML文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆