select a single node from an xml file ?

G

Guest

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>
 
P

Peter Theill

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
 
K

Ken Cox [Microsoft MVP]

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
 
P

Peter Theill

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
 
K

Ken Cox [Microsoft MVP]

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,008
Messages
2,570,269
Members
46,870
Latest member
hemasindhura

Latest Threads

Top