Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SelectSingleNode issue

SelectSingleNode issue

Scheduled Pinned Locked Moved C#
xmlhelpquestion
19 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T tiwal

    problems on the xml ?I wonder which kind of problems could they be ... it is such a simple xml....

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #8

    Really? Does your XML file specify an encoding? Does the file actually use that encoding? ...or are you making assumptions about those things?

    A guide to posting questions on CodeProject

    Click this: Asking questions is a skill. Seriously, do it.
    Dave Kreskowiak

    T 1 Reply Last reply
    0
    • D Dave Kreskowiak

      Really? Does your XML file specify an encoding? Does the file actually use that encoding? ...or are you making assumptions about those things?

      A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      T Offline
      T Offline
      tiwal
      wrote on last edited by
      #9

      You mean things like the following : ? in the starting version it was present, but when I removed it to make a test, I noticed no change at all, so I left it out....

      Richard DeemingR 1 Reply Last reply
      0
      • T tiwal

        You mean things like the following : ? in the starting version it was present, but when I removed it to make a test, I noticed no change at all, so I left it out....

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #10

        How have you defined the LanguageDoc variable? What's in the fPath variable? Are you absolutely certain that the fPath variable points to the same XML document you've posted?


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        T 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          How have you defined the LanguageDoc variable? What's in the fPath variable? Are you absolutely certain that the fPath variable points to the same XML document you've posted?


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          T Offline
          T Offline
          tiwal
          wrote on last edited by
          #11

          Here it is :

          String LanguageFileName = "\\Language" + Session["Language"].ToString() + ".xml";

          fPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath);

          fPath += LanguageFileName;
          XmlDocument LanguageDoc = new XmlDocument();

          The "Language" session variable is passed by another page that redirects to this one: for now it is "English" , so in the end you get LanguageFileName = "LanguageEnglish.xml" and the fPath is where the file actually is .

          Richard DeemingR 1 Reply Last reply
          0
          • T tiwal

            Here it is :

            String LanguageFileName = "\\Language" + Session["Language"].ToString() + ".xml";

            fPath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath);

            fPath += LanguageFileName;
            XmlDocument LanguageDoc = new XmlDocument();

            The "Language" session variable is passed by another page that redirects to this one: for now it is "English" , so in the end you get LanguageFileName = "LanguageEnglish.xml" and the fPath is where the file actually is .

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #12

            Assuming this is an ASP.NET application, I'd be inclined to use Server.MapPath with an app-relative path (eg: "~/Language" + Session["Language"] + ".xml") rather than the assembly's CodeBase. Other than that, I'm struggling to see why your code isn't working.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              The code you've posted works fine for me:

              using System;
              using System.Xml;

              public class Program
              {
              public static void Main()
              {
              var LanguageDoc = new XmlDocument();
              LanguageDoc.LoadXml(@"<language>
              <main>
              <aboutlink>About</aboutlink>
              <registrationlink>Registration</registrationlink>
              <loginlink>Enter</loginlink>
              </main>
              <about>
              <description>This site is about friends and games </description>
              </about>
              </language>");

                  XmlNode root = LanguageDoc.DocumentElement;
                  XmlNode main = root.SelectSingleNode("/language/main");
                  XmlNode about = main.SelectSingleNode("aboutlink");
                  Console.WriteLine("About: {0}", about == null ? "NULL" : about.InnerText);
                  XmlNode registration = main.SelectSingleNode("registrationlink");
                  Console.WriteLine("Registration: {0}", registration == null ? "NULL" : registration.InnerText);
                  XmlNode login = main.SelectSingleNode("loginlink");
                  Console.WriteLine("Login: {0}", login == null ? "NULL" : login.InnerText);
              }
              

              }
              /*
              Output:
              About: About
              Registration: Registration
              Login: Enter
              */

              https://dotnetfiddle.net/DZBjv8[^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              T Offline
              T Offline
              tiwal
              wrote on last edited by
              #13

              Richard you didn't try exactly what I did : you loaded the xml as a string . I did the same thing and it works fine. Try doing as I do, that is loading a real xml file : I bet it won't work either....

              Richard DeemingR 1 Reply Last reply
              0
              • T tiwal

                I have a very basic xml document :

                About
                Registration
                Enter
                

                This site is about friends and games

                I just want to grab the innerText of the , and nodes and assign them to three different Hyperlinks in a web page . So here is what I do (assuming fPath contains the path to this xml ile) :

                LanguageDoc.Load(fPath);
                XmlNode root = LanguageDoc.DocumentElement;
                main = root.SelectSingleNode("/language/main");
                HyperLinkAbout.Text = main.SelectSingleNode("aboutlink").InnerText;
                HyperLinkRegistration.Text = main.SelectSingleNode("registrationlink").InnerText;
                HyperLinkLogin.Text = main.SelectSingleNode("loginlink").InnerText;

                What's happening is that after the first call to main.SelectSingleNode ("aboutlink").InnerText (which is successful and gives me the right value to assign to the first hyperlink), the second call returns a null object, so the call to the InnetText method fails and I get an exception. The element searched for exists in the xml, and I think the XPath path to that element is correct. So, what's happening ?

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #14

                That's not an XML document. That's a fragment of an XML document - you're missing the header. Do you have any XML namespaces, or XSD's referenced?

                Richard DeemingR 1 Reply Last reply
                0
                • T tiwal

                  Richard you didn't try exactly what I did : you loaded the xml as a string . I did the same thing and it works fine. Try doing as I do, that is loading a real xml file : I bet it won't work either....

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #15

                  Nope - loading from an XML file gives the same output:

                  var LanguageDoc = new XmlDocument();
                  LanguageDoc.Load(fPath);

                  XmlNode root = LanguageDoc.DocumentElement;
                  XmlNode main = root.SelectSingleNode("/language/main");
                  XmlNode about = main.SelectSingleNode("aboutlink");
                  Console.WriteLine("About: {0}", about == null ? "NULL" : about.InnerText);
                  XmlNode registration = main.SelectSingleNode("registrationlink");
                  Console.WriteLine("Registration: {0}", registration == null ? "NULL" : registration.InnerText);
                  XmlNode login = main.SelectSingleNode("loginlink");
                  Console.WriteLine("Login: {0}", login == null ? "NULL" : login.InnerText);

                  /*
                  Output:

                  About: About
                  Registration: Registration
                  Login: Enter
                  */


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    That's not an XML document. That's a fragment of an XML document - you're missing the header. Do you have any XML namespaces, or XSD's referenced?

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #16

                    If you mean the XML declaration (<?xml version="1.0" encoding="UTF-8"?>), that's not required.

                    XML Declaration - MSDN[^]

                    The XML declaration is not required, however, if used it must be the first line in the document and no other content or white space can precede it.

                    All of the .NET XML libraries will happily parse an XML document without an XML declaration.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    P 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      If you mean the XML declaration (<?xml version="1.0" encoding="UTF-8"?>), that's not required.

                      XML Declaration - MSDN[^]

                      The XML declaration is not required, however, if used it must be the first line in the document and no other content or white space can precede it.

                      All of the .NET XML libraries will happily parse an XML document without an XML declaration.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #17

                      The fact he didn't include this part makes me think that other parts are missing (hence why I raised this). I know the declaration part isn't needed, but if there's any namespace in there, that will be needed.

                      Richard DeemingR 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        The fact he didn't include this part makes me think that other parts are missing (hence why I raised this). I know the declaration part isn't needed, but if there's any namespace in there, that will be needed.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #18

                        According to this message[^], he originally had:

                        <?xml version="1.0" encoding="utf-8" ?>

                        but he removed it to see if it made any difference.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        P 1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          According to this message[^], he originally had:

                          <?xml version="1.0" encoding="utf-8" ?>

                          but he removed it to see if it made any difference.


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #19

                          Ah, okay - I based this off the first post.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups