M
Mike
Hello,
I have a custom configuration file that I use to load some data for users to modify (no DB involved). Since this data is used in different GUI components (comboboxes, textboxes, etc.), I want to be able to read just some parts of the XML file when required (or keep the whole file in memory to be processed "on demand").
Here is a part of the XML file:
<?xml version="1.0" standalone="no"?>
<servers xmlns="http://tempuri.org/Servers.xsd">
<server>
<name>myserver_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>myserver_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>
........
........
I would like, for example, to only read the "<server>" element's text ("myserver_1" and "myserver_2"). I used the following code, but cannot make it work:
// Create an XmlDocument
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
//Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("server");
foreach (XmlNode isbn in nodeList)
{
Console.WriteLine(isbn.InnerText);
}
// Write out data as XML
// doc.Save(fileName);
return doc;
}
What is wrong with my code? Is this the best approach considering the scenario I described above? I am really stuck....
Thanks.
Mike
I have a custom configuration file that I use to load some data for users to modify (no DB involved). Since this data is used in different GUI components (comboboxes, textboxes, etc.), I want to be able to read just some parts of the XML file when required (or keep the whole file in memory to be processed "on demand").
Here is a part of the XML file:
<?xml version="1.0" standalone="no"?>
<servers xmlns="http://tempuri.org/Servers.xsd">
<server>
<name>myserver_1</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>
<name />
<organization />
<email />
</user>
<forums>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
<forum>
<name />
<subscribed />
</forum>
</forums>
</server>
<server>
<name>myserver_2</name>
<portNumber>119</portNumber>
<description>n/a</description>
<lastAccessed>n/a</lastAccessed>
<messageCount>0</messageCount>
<user>
........
........
I would like, for example, to only read the "<server>" element's text ("myserver_1" and "myserver_2"). I used the following code, but cannot make it work:
// Create an XmlDocument
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
//Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("server");
foreach (XmlNode isbn in nodeList)
{
Console.WriteLine(isbn.InnerText);
}
// Write out data as XML
// doc.Save(fileName);
return doc;
}
What is wrong with my code? Is this the best approach considering the scenario I described above? I am really stuck....
Thanks.
Mike