Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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
<?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
C#
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;
                }
            }
        }
    }
}
Posted
Updated 22-Feb-11 23:06pm
v7
Comments
Indivara 23-Feb-11 2:22am    
Added <pre> block
Sandeep Mewara 23-Feb-11 2:29am    
At the time of fixing PRE tags, if you can look at modifying little text-speak, extra spaces and sentence formation too, it would be great. :thumbs up:
Saranya Natesan 23-Feb-11 2:44am    
The above stuff is works perfectly in WindowsForms Application.but it shows error in Smart project application mean mobile application.this is what my problem.
Indivara 23-Feb-11 7:43am    
Yes, yes I know. I got distracted at the time...
Saranya Natesan 23-Feb-11 2:43am    
The above stuff is works perfectly in WindowsForms Application.but it shows error in Smart project application mean mobile application.this is what my problem.

1 solution

"//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
.
 
Share this answer
 
v3
Comments
Saranya Natesan 23-Feb-11 2:42am    
The above stuff is working perfectly in WindowsFormsApplication.but it shows error in Smart project application mean mobile application
Sergey Alexandrovich Kryukov 23-Feb-11 3:35am    
Do you need instructions how to write correct URL or file path? How do you know where your file is? It is not found.
--SA
Saranya Natesan 23-Feb-11 3:44am    
the same program exactly retrives the xml document while am running in windows application.but it shows error in windows mobile application.
Sergey Alexandrovich Kryukov 23-Feb-11 3:56am    
I can read. Is says nothing. Don't do "same", do correctly!
Again, should I show you how to write correct URL or file name?
You working directory, file structure, path names should be a bit different.
You try to write relative path relative working directory (but incorrectly, "//" should be removed).

But! Nothing guarantees working directory! It depends on how you run the application.
File name should be calculated relative to "special directory" (see Environment class) like user's Application Data, or relative to Assembly.GetEntryAssembly().Location (!!!). Absolute path will also work, but this is absolutely non-portable, so would be too bad style. But for test/experiment you can try it.

Please, do something instead of repeating again and again your invalid argument about "the same". I just explained how it can be not the same.

--SA
Sergey Alexandrovich Kryukov 23-Feb-11 5:07am    
OK, I added detailed explanation in my Answer, but please understand that you should identify where your files are by yourself and know differences in your system. The differences themselves are not important, you problem is wrong logical inference, which led to your invalid "the same" argument. Your code/data are not written in environment-proof manner...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900