foreach loop confusion

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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());
}
 
Art,

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


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Art said:
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());
}
and surely enough it is not - why?
 
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 said:
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());
}
and surely enough it is not - why?
 
Shakir Hussain said:
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.
 
Art said:
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());
}

Could you post the *exact* error message? A short but complete example
(including the XML file) would be helpful, too.
 
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.
 
Art said:
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.
 
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]
- (e-mail address removed)

Art said:
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>
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.
 
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
 
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 said:
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]
- (e-mail address removed)

Art said:
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>
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.
 
Back
Top