473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

select a single node from an xml file ?

how do i select a single node value from the list?

ive tried with this, but it dosent work =/

private string FetchFieldFromXml(string strId)

{

string strText = "";

XmlNode fetchnode;

XmlDocument mydoc = new XmlDocument();

try

{

mydoc.Load(strXmlFieldPath);

fetchnode =
mydoc.SelectSingleNode("//fields//formmonthnames//monthnames[id=" + strId +
"]");

strText = fetchnode.InnerText;

}

catch (Exception e)

{

}

return strText;

}


xmlfile looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>

<fields>

<formmonthnames>

<monthnames id="01">January</monthnames>

<monthnames id="02">February</monthnames>

<monthnames id="03">March</monthnames>

<monthnames id="04">April</monthnames>

<monthnames id="05">May</monthnames>

<monthnames id="06">June</monthnames>

<monthnames id="07">July</monthnames>

<monthnames id="08">August</monthnames>

<monthnames id="09">September</monthnames>

<monthnames id="10">October</monthnames>

<monthnames id="11">November</monthnames>

<monthnames id="12">December</monthnames>

</formmonthnames>

</field>
Nov 18 '05 #1
4 2948
If that's the code you're using you're probably getting an exception
(you ignore) on the "myDoc.Load" method since the Xml is invalid. The
<fields> node is started but ends with </field>"

Rgd,
Peter Theill

Nov 18 '05 #2
Hi Patrick,

Before you go too far, make sure your XML is well formed. The starting
<fields> tag doesn't match the closing </field>.

Ken
"Patrick F" <Pa******@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
how do i select a single node value from the list?

ive tried with this, but it dosent work =/

private string FetchFieldFromXml(string strId)

{

string strText = "";

XmlNode fetchnode;

XmlDocument mydoc = new XmlDocument();

try

{

mydoc.Load(strXmlFieldPath);

fetchnode =
mydoc.SelectSingleNode("//fields//formmonthnames//monthnames[id=" + strId
+
"]");

strText = fetchnode.InnerText;

}

catch (Exception e)

{

}

return strText;

}


xmlfile looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>

<fields>

<formmonthnames>

<monthnames id="01">January</monthnames>

<monthnames id="02">February</monthnames>

<monthnames id="03">March</monthnames>

<monthnames id="04">April</monthnames>

<monthnames id="05">May</monthnames>

<monthnames id="06">June</monthnames>

<monthnames id="07">July</monthnames>

<monthnames id="08">August</monthnames>

<monthnames id="09">September</monthnames>

<monthnames id="10">October</monthnames>

<monthnames id="11">November</monthnames>

<monthnames id="12">December</monthnames>

</formmonthnames>

</field>


Nov 18 '05 #3
If that's the code you're using you're probably getting an exception
(you ignore) on the "myDoc.Load" method since the Xml is invalid. The
<fields> node is started but ends with </field>"

Rgd,
Peter Theill

Nov 18 '05 #4
Hi Patrick,

You weren't using the correct syntax to locate using an attribute value. You
need to use @id= for that.

Here's some code that should get you going:

private void Page_Load(object sender, System.EventArgs e)
{
Response.Write(FetchFieldFromXml("09"));
}

private string FetchFieldFromXml(string strId)
{
XmlNode fetchnode;
XmlDocument mydoc = new XmlDocument();
mydoc.Load(Server.MapPath(@"frm.xml"));
fetchnode =
mydoc.SelectSingleNode(@"/fields/formmonthnames/monthnames[@id='" + strId +
"']");
return fetchnode.InnerText;
}

<?xml version="1.0" encoding="iso-8859-1" ?>
<fields>
<formmonthnames>
<monthnames id="01">January</monthnames>
<monthnames id="02">February</monthnames>
<monthnames id="03">March</monthnames>
<monthnames id="04">April</monthnames>
<monthnames id="05">May</monthnames>
<monthnames id="06">June</monthnames>
<monthnames id="07">July</monthnames>
<monthnames id="08">August</monthnames>
<monthnames id="09">September</monthnames>
<monthnames id="10">October</monthnames>
<monthnames id="11">November</monthnames>
<monthnames id="12">December</monthnames>
</formmonthnames>
</fields>
Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

"Patrick F" <Pa******@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
how do i select a single node value from the list?

ive tried with this, but it dosent work =/

private string FetchFieldFromXml(string strId)

{

string strText = "";

XmlNode fetchnode;

XmlDocument mydoc = new XmlDocument();

try

{

mydoc.Load(strXmlFieldPath);

fetchnode =
mydoc.SelectSingleNode("//fields//formmonthnames//monthnames[id=" + strId
+
"]");

strText = fetchnode.InnerText;

}

catch (Exception e)

{

}

return strText;

}


xmlfile looks like this:

<?xml version="1.0" encoding="iso-8859-1"?>

<fields>

<formmonthnames>

<monthnames id="01">January</monthnames>

<monthnames id="02">February</monthnames>

<monthnames id="03">March</monthnames>

<monthnames id="04">April</monthnames>

<monthnames id="05">May</monthnames>

<monthnames id="06">June</monthnames>

<monthnames id="07">July</monthnames>

<monthnames id="08">August</monthnames>

<monthnames id="09">September</monthnames>

<monthnames id="10">October</monthnames>

<monthnames id="11">November</monthnames>

<monthnames id="12">December</monthnames>

</formmonthnames>

</field>


Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Colman | last post by:
Howdy all! I guess I'm a newbie, because I am stumped (or maybe just too durned tired). Here's what I got... CREATE TABLE `nodecat_map` ( `nodecat_id` mediumint(8) unsigned NOT NULL...
2
by: Piet | last post by:
Hi there, I have managed to write a small java program that display an xml file in a JTree. This was achieved by defining a suitable tree model. THe program works...somehow. The nodes (elements...
1
by: wabr7030 | last post by:
I have two xml file 1) xml source: <Indici> <F1>xxx</F1> <F2>yyy</F2> </Indici> 2) File colsDef.xml:
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
1
by: Wallace | last post by:
Hi all, I have a problem on validating a xml fragment using a single namespace schema which spread across multiple schema files using include in the master schema file. No matter how I change...
8
by: David Thielen | last post by:
Hi; I need to be able to create a single xml node in utf-8 and do it efficiently as this is called a lot (reason below). What is the best way to do this? I need to end up with a string like...
16
by: Brian D | last post by:
I have a multiple select list that is created dynamically based on a previous selection on an asp page. The first thing I do is to clear the curent option list by ...
2
by: sk.rasheedfarhan | last post by:
Hi , I have to extract a xml file line by line. Here is my xml Step1: <category name= "name" guid="{guid}"state="enabled"> Step2: <rules> Step3: <rule name="rule name" guid="{guid}"...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.