By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

foreach loop confusion

P: n/a
Art
Hello,
Here is a fragment of my code, which fails and I don't know why.
---------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\mydoc.xml");
foreach (XmlNode objNode in doc.SelectNodes("author"))
{
System.WriteLine(objNode.Value.ToString());
}
---------------------------------------------------------------
I get an error saying that objNode is not set to XmlNode. I step thru code and surely enough it is not - why?
Nov 16 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Art,

Can you post the document that you are parsing and scanning through?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:17**********************************@microsof t.com...
Hello,
Here is a fragment of my code, which fails and I don't know why.
---------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\mydoc.xml");
foreach (XmlNode objNode in doc.SelectNodes("author"))
{
System.WriteLine(objNode.Value.ToString());
}
---------------------------------------------------------------
I get an error saying that objNode is not set to XmlNode. I step thru code and surely enough it is not - why?

Nov 16 '05 #2

P: n/a
foreach loop is a beautiful concept which saves lotsa headache and
unnecessary variable assignments. This is the quickest way to iterate a
collection. However, please keep in mind the following

1. You cannot remove a value from the collection while reading
2. You cannot add values while reading
3. You cannot refresh values while reading

To solve ur problem do this.

XmlNodeList nList = doc.SelectNodes("author");

foreach (XmlNode objNode in nList)
{
System.WriteLine(objNode.Value.ToString());
}
//note: the following code will throw execption
foreach (XmlNode objNode in nList)
{
nList.Remove(0); //Collection value cannot be modified
nList.Add(newNode);//collection value cannot be modified.
}

Shak.

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:17**********************************@microsof t.com...
Hello,
Here is a fragment of my code, which fails and I don't know why.
---------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\mydoc.xml");
foreach (XmlNode objNode in doc.SelectNodes("author"))
{
System.WriteLine(objNode.Value.ToString());
}
---------------------------------------------------------------
I get an error saying that objNode is not set to XmlNode. I step thru code and surely enough it is not - why?

Nov 16 '05 #3

P: n/a
Shakir Hussain <sh***********@hotmail.com> wrote:
foreach loop is a beautiful concept which saves lotsa headache and
unnecessary variable assignments. This is the quickest way to iterate a
collection. However, please keep in mind the following

1. You cannot remove a value from the collection while reading
2. You cannot add values while reading
3. You cannot refresh values while reading

To solve ur problem do this.

XmlNodeList nList = doc.SelectNodes("author");

foreach (XmlNode objNode in nList)
{
System.WriteLine(objNode.Value.ToString());
}


That shouldn't in any way affect the code which was posted, however, as
the posted code didn't change the collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
Art <Ar*@discussions.microsoft.com> wrote:
Here is a fragment of my code, which fails and I don't know why.
---------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\mydoc.xml");
foreach (XmlNode objNode in doc.SelectNodes("author"))
{
System.WriteLine(objNode.Value.ToString());
}
---------------------------------------------------------------
I get an error saying that objNode is not set to XmlNode. I step thru
code and surely enough it is not - why?


Could you post the *exact* error message? A short but complete example
(including the XML file) would be helpful, too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
Art
Shak,
I'm going to try your suggestion as I'm not trying to modify XML file here - just reading the values, which will go into DataTable.

"Shakir Hussain" wrote:
foreach loop is a beautiful concept which saves lotsa headache and
unnecessary variable assignments. This is the quickest way to iterate a
collection. However, please keep in mind the following

1. You cannot remove a value from the collection while reading
2. You cannot add values while reading
3. You cannot refresh values while reading

To solve ur problem do this.

XmlNodeList nList = doc.SelectNodes("author");

foreach (XmlNode objNode in nList)
{
System.WriteLine(objNode.Value.ToString());
}
//note: the following code will throw execption
foreach (XmlNode objNode in nList)
{
nList.Remove(0); //Collection value cannot be modified
nList.Add(newNode);//collection value cannot be modified.
}

Shak.

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:17**********************************@microsof t.com...
Hello,
Here is a fragment of my code, which fails and I don't know why.
---------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\mydoc.xml");
foreach (XmlNode objNode in doc.SelectNodes("author"))
{
System.WriteLine(objNode.Value.ToString());
}
---------------------------------------------------------------
I get an error saying that objNode is not set to XmlNode. I step thru code

and surely enough it is not - why?


Nov 16 '05 #6

P: n/a
Art <Ar*@discussions.microsoft.com> wrote:
The above is the exact structure I'm working with. More, I was trying
to extract all the "filename" nodes and just print their values like
this:

XmlDocument doc = new XmlDocument(); - I'm OK here
doc.Load <above XML file:>; - I'm OK here
foreach (XmlNode objNode in doc.SelectNodes("//textfiles/files/filename"))
{
System.WriteLine(objNode.Value.ToString()) - Failure here
// Error - Object reference not set to an instance of an object
}


Ah, that's not the error you talked about before. That sounds like a
NullReferenceException, which just means that objNode.Value is null.
(Assuming objNode itself isn't null, which seems unlikely to me.)

It's entirely reasonable that objNode.Value is null, however, as you'll
have selected a load of elements, and (as the documentation for
XmlNode.Value states) the Value of an element is null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
Art,

I don't see an author element or attribute there, so perhaps the
SelectNode method is returning null (which would definitely cause an
exception).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
Nicholas,
The document is rather large but here is an excerpt:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<config>
<textfiles>
<files path="/">
<filename att1="1" att2="2">file1.txt</filename>
{lots more nodes like <filename>}
</files>
<files path="/Secure">
<filename att1="1" att2="2">file1.txt</filename>
{lots more nodes like <filename>}
</files>
{lots more nodes like <files>}
</textfiles>
</config>
----------------------------------------------

The above is the exact structure I'm working with. More, I was trying to extract all the "filename" nodes and just print their values like this:
XmlDocument doc = new XmlDocument(); - I'm OK here
doc.Load <above XML file:>; - I'm OK here
foreach (XmlNode objNode in doc.SelectNodes("//textfiles/files/filename"))
{
System.WriteLine(objNode.Value.ToString()) - Failure here
// Error - Object reference not set to an instance of an object
}

I have also tried iterating via for loop and using index in NodeList.Item(i).Value etc. but the result was the same.
Art

"Nicholas Paldino [.NET/C# MVP]" wrote:
Art,

Can you post the document that you are parsing and scanning through?

Nov 16 '05 #8

P: n/a
Art
John,
Yes, your suggestion was right on a target. What I should have done was to use "InnerText" property to get a text value for that node instead of ".Value.ToString()", which really was null.
Art

"Jon Skeet [C# MVP]" wrote:
Art <Ar*@discussions.microsoft.com> wrote:
The above is the exact structure I'm working with. More, I was trying
to extract all the "filename" nodes and just print their values like
this:

XmlDocument doc = new XmlDocument(); - I'm OK here
doc.Load <above XML file:>; - I'm OK here
foreach (XmlNode objNode in doc.SelectNodes("//textfiles/files/filename"))
{
System.WriteLine(objNode.Value.ToString()) - Failure here
// Error - Object reference not set to an instance of an object
}


Ah, that's not the error you talked about before. That sounds like a
NullReferenceException, which just means that objNode.Value is null.
(Assuming objNode itself isn't null, which seems unlikely to me.)

It's entirely reasonable that objNode.Value is null, however, as you'll
have selected a load of elements, and (as the documentation for
XmlNode.Value states) the Value of an element is null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9

P: n/a
Art
Nicholas,
Sorry about the confusion related to XML ... I have been looking on the web for examples on how to do this and lots of places had the typical author/title XML sample file. I guess I quoted what was just on my mind.
Thanks for your time and help. Problem was related to Node.Value vs. Node.InnerText property. I thought that Value would give you text value of a node which was not the case.
Thanks.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Art,

I don't see an author element or attribute there, so perhaps the
SelectNode method is returning null (which would definitely cause an
exception).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Art" <Ar*@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
Nicholas,
The document is rather large but here is an excerpt:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<config>
<textfiles>
<files path="/">
<filename att1="1" att2="2">file1.txt</filename>
{lots more nodes like <filename>}
</files>
<files path="/Secure">
<filename att1="1" att2="2">file1.txt</filename>
{lots more nodes like <filename>}
</files>
{lots more nodes like <files>}
</textfiles>
</config>
----------------------------------------------

The above is the exact structure I'm working with. More, I was trying to

extract all the "filename" nodes and just print their values like this:

XmlDocument doc = new XmlDocument(); - I'm OK here
doc.Load <above XML file:>; - I'm OK here
foreach (XmlNode objNode in doc.SelectNodes("//textfiles/files/filename"))
{
System.WriteLine(objNode.Value.ToString()) - Failure here
// Error - Object reference not set to an instance of an object
}

I have also tried iterating via for loop and using index in

NodeList.Item(i).Value etc. but the result was the same.

Art

"Nicholas Paldino [.NET/C# MVP]" wrote:
Art,

Can you post the document that you are parsing and scanning through?


Nov 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.