XML Node Retrieve

S

Soulless

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.
 
A

Alberto Poblacion

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
 
S

Soulless

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?
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.- Hide quoted text -

- Show quoted text -

Thank you! worked like a charm :)
 

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

Top