Thanks Dennis. I made the appropriate suggestions, as you suggested, and it is now working!
Regards.
Mike
"Dennis Myrén" <de****@oslokb.no> wrote in message news:0e******************@news2.e.nsc.no...
Yeah, you are right;
a namespace prefix other than empty needs to be specified for the XmlNamespaceManager.
In the previous mail, i used an empty string as prefix.
In this example, i use "servers" as the prefix.
You can use any prefix you like.
Than, in any XPath evaluation, use that prefix followed by a colon.
It is only important that the URI matches (in your case
http://tempuri.org/Servers.xsd).
This should work:
<snippet>
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlElement root = doc.DocumentElement;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("servers", "http://tempuri.org/Servers.xsd");
XmlNodeList nl = root.SelectNodes("servers:server/servers:name", ns);
Console.WriteLine(nl.Count);
foreach (XmlNode nameNode in nl)
{
Console.WriteLine(nameNode.InnerText);
}
Console.Read();
return;
</snippet>
See
http://www.codeguru.com/Csharp/Cshar...cle.php/c6737/
for information about XML validation with XML schema in .NET.
Regards, Dennis
"Mike" <no****@nospam.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Dennis, it doesn't seem to work.
I actually copied and pasted the namespace URI. I am new to XML, so I don't know what I should put in there. In Visual Studio.NET I created a schema from my XML file. What should I do to use this newly created schema?
Thanks.
Mike
"Dennis Myrén" <de****@oslokb.no> wrote in message news:qq******************@news2.e.nsc.no...
if you need to read the name element you would do something like:
XmlNodeList nl = root.SelectNodes("server/name");
foreach (XmlNode nameNode in nl)
{
Console.WriteLine(nameNode.InnerText);
}
_BUT_, i see that you use a default namespace URI (xmlns="http://tempuri.org/Servers.xsd").
So, you need to create an XmlNamespaceManager for resolving this URI. So this should work:
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace(string.Empty, "http://tempuri.org/Servers.xsd");
XmlNodeList nl = root.SelectNodes("server/name", ns);
foreach (XmlNode nameNode in nl)
{
Console.WriteLine(nameNode.InnerText);
}
"Mike" <no****@nospam.com> wrote in message news:e8**************@TK2MSFTNGP10.phx.gbl...
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