Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

XML Node Retrieve

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Soulless - 23 Oct 2007 17:55 GMT
Hi, I am trying to retrieve a value from a node in an XML document and
cannot.. .any ideas?

Here is my code:

string isDBConfig =
System.IO.Directory.GetCurrentDirectory().ToString() + "\
\dsconfig.XML";

           XmlDocument doc = new XmlDocument();
           doc.Load(isDBConfig);

           /// I use this line to show me that the file truly is
loaded...
           Console.WriteLine(doc.InnerXml.ToString());

        // I have tried using the node and the full path...
Appconfig
always returns 'null'...
           XmlNode Appconfig = doc.SelectSingleNode("//
logging_option");

           string s;

/// I don't worry too much about this because of the 'null' above...
when it gets here it crashed because of the null...
           s = Appconfig.SelectSingleNode("//
logging_option").InnerText;

                //XmlNode option =
doc.GetElementsByTagName("logging_option")[0];

           Console.WriteLine(s);

Here is part of the file:

<?xml version="1.0" standalone="yes" ?>
- <dsConfig xmlns="http://tempuri.org/dsConfig.xsd">
- <AppConfig>
 <extract_directory>c:\temp</extract_directory>
 <logging_option>E</logging_option>
 </AppConfig>

I want 'logging_option' value 'E' returned.

I cannot figure out what i'm doing wrong.
Thanks.
Alberto Poblacion - 23 Oct 2007 18:18 GMT
The problem is that the XML file defines a default namespace, and you are
not adding the namespace when you perform the query. You do this with a
NameSpaceManager:

          XmlDocument doc = new XmlDocument();
          doc.Load(isDBConfig);
          XmlNamespaceManager nsManager = new
XmlNamespaceManager(doc.NameTable);
          nsManager.AddNamespace("d", "http://tempuri.org/dsConfig.xsd");
          XmlNode Appconfig = doc.SelectSingleNode("//d:logging_option",
nsManager);
          //Now it should find your node instead of returning null

> Hi, I am trying to retrieve a value from a node in an XML document and
> cannot.. .any ideas?
[quoted text clipped - 42 lines]
>
> I cannot figure out what i'm doing wrong.
Soulless - 23 Oct 2007 20:30 GMT
On Oct 23, 12:18 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> The problem is that the XML file defines a default namespace, and you are
> not adding the namespace when you perform the query. You do this with a
[quoted text clipped - 57 lines]
>
> - Show quoted text -

Thank you!  worked like a charm :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.