473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML problem


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

Nov 16 '05 #1
4 1495
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

Nov 16 '05 #2

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

Nov 16 '05 #3
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

Nov 16 '05 #4

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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.