Advertisement

01.15.2008 at 04:11AM PST, ID: 23083574
[x]
Attachment Details

XPath Queries Using XmlDocument and SelectSingleNode

Asked by SketchySteve in Microsoft Visual C#.Net, Extensible Stylesheet Language Transformation (XSLT), .Net Editors & IDEs

Tags: C#

Hi guys and thanks for looking,

I have a new project where fixed text can be accessed by either AJAX or a .NET page. I decided to place the text in an XML file to avoid duplication.

Initially I used an int ID and a method "GetErrorMessageById" to retrieve the item of text. This works fine and both the call and the method are shown in the code snippet.

then I decided to use a string value instead using the method GetErrorMessageByFieldId which is alos illustrated below. This is the one I am having an issue with as it wont return any nodes when called.

Please cna some one let me know what i'm doing wrong here and suggest a fix or a work around.

All code and XML below. Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
public sealed class ProjectSpecific
    {
        public static string GetErrorMessageById(int nMessageID)
        {
            XmlDocument xmlErrorMessages = GetXMLErrorMessagesDocument();
            return xmlErrorMessages.DocumentElement.SelectSingleNode("message[id=" + nMessageID.ToString() + @"]").FirstChild.NextSibling.InnerText;
        }
        public static string GetErrorMessageByFieldId(string strFieldID)
        {
            XmlDocument xmlErrorMessages = GetXMLErrorMessagesDocument();
            return xmlErrorMessages.DocumentElement.SelectSingleNode("message[field_id=" + strFieldID + @"]").FirstChild.NextSibling.InnerText;
        }
 
        private static XmlDocument GetXMLErrorMessagesDocument()
        {
            XmlDocument xmlErrorMessages = new XmlDocument();
 
            if (HttpContext.Current.Cache["ErrorMsg_XML"] != null)
                xmlErrorMessages = (XmlDocument)HttpContext.Current.Cache["ErrorMsg_XML"];
            else
            {
                xmlErrorMessages.Load(HttpContext.Current.Server.MapPath("~/includes/xml/ErrorMessageCopy.xml"));
                HttpContext.Current.Cache["ErrorMsg_XML"] = xmlErrorMessages;
            }
            return xmlErrorMessages;
        }
    }
 
______________________________________________________________
 
 
string str1 = Utils.ProjectSpecific.GetErrorMessageById(1);
 
string str2 = Utils.ProjectSpecific.GetErrorMessageByFieldId("tbForename");
 
<?xml version="1.0" encoding="utf-8" ?>
 
<messages>
  <message>
    <id>0</id>
    <text>You must supply your forename</text>
    <fieldid>tbForename</fieldid>
  </message>
  <message>
    <id>1</id>
    <text>You must supply your surname</text>
    <field_id>tbSurname</field_id>
  </message>
  <message>
    <id>2</id>
    <text>You must supply your home address</text>
    <field_id>tbHomeAddress</field_id>
  </message>
  <message>
    <id>3</id>
    <text>You must supply your mobile number</text>
    <field_id>tbMobileNumber</field_id>
  </message>
  <message>
    <id>4</id>
    <text>You must supply a valid email address</text>
    <field_id>tbEmailAddress</field_id>
  </message>
</messages>
 
Loading Advertisement...
 
[+][-]01.15.2008 at 04:53AM PST, ID: 20662025

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.15.2008 at 04:53AM PST, ID: 20662032

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, Extensible Stylesheet Language Transformation (XSLT), .Net Editors & IDEs
Tags: C#
Sign Up Now!
Solution Provided By: existenz2
Participating Experts: 1
Solution Grade: A
 
 
[+][-]01.15.2008 at 05:23AM PST, ID: 20662273

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628