SelectSingleNode issue
-
No, you don't. The better problem is that the XML file you're looking at has the problem, or the code you posted isn't what you actually have running.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
problems on the xml ?I wonder which kind of problems could they be ... it is such a simple xml....
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 -
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 -
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....
How have you defined the
LanguageDoc
variable? What's in thefPath
variable? Are you absolutely certain that thefPath
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
-
How have you defined the
LanguageDoc
variable? What's in thefPath
variable? Are you absolutely certain that thefPath
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
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 .
-
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 .
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'sCodeBase
. 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
-
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
-
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 ?
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 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....
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
-
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?
If you mean the XML declaration (
<?xml version="1.0" encoding="UTF-8"?>
), that's not required.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
-
If you mean the XML declaration (
<?xml version="1.0" encoding="UTF-8"?>
), that's not required.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
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.
-
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.
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
-
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
Ah, okay - I based this off the first post.