foreach循环混乱 [英] foreach loop confusion

查看:143
本文介绍了foreach循环混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

这是我的代码的一个片段,它失败了,我不知道为什么。

---------- -----------------------

XmlDocument doc = new XmlDocument();

doc.Load (@" C:\ mydoc.xml");

foreach(doc.SelectNodes中的XmlNode objNode(" author"))

{

System.WriteLine(objNode.Value.ToString());

}

---------------- -----------------------------------------------

我收到一条错误消息,说objNode未设置为XmlNode。通过代码,我肯定不是 - 为什么?

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?

推荐答案

艺术,


你能来吗?发布您正在解析和扫描的文档?

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Art" < Ar*@discussions.microsoft.com>在消息中写道

news:17 ********************************** @ microsof t.com ...
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...
你好,
这是我代码的一个片段,它失败了,我不知道为什么。
------- --------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@" C :\ mydoc.xml");
foreach(doc.SelectNodes中的XmlNode objNode(" author"))
{/> System.WriteLine(objNode.Value.ToString());
}
----------------------------------------- ----------------------
我收到一个错误,说objNode没有设置为XmlNode。我通过代码
,但肯定不是 - 为什么?
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?



foreach循环是一个美丽的概念,可以节省很多头痛和

不必要的变量赋值。这是迭代

集合的最快方法。但是,请记住以下内容


1.阅读时无法从集合中删除值

2.阅读时无法添加值/>
3.阅读时无法刷新值


要解决问题,请执行此操作。


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


foreach(nList中的XmlNode objNode)

{

System.WriteLine(objNode。 Value.ToString());

}

//注意:以下代码将抛出execption

foreach(nList中的XmlNode objNode)

{

nList.Remove(0); //集合值无法修改

nList.Add(newNode); //集合值无法修改。

}


Shak。


" Art" < Ar*@discussions.microsoft.com>在消息中写道

news:17 ********************************** @ microsof t.com ...
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...
你好,
这是我代码的一个片段,它失败了,我不知道为什么。
------- --------------------------
XmlDocument doc = new XmlDocument();
doc.Load(@" C :\ mydoc.xml");
foreach(doc.SelectNodes中的XmlNode objNode(" author"))
{/> System.WriteLine(objNode.Value.ToString());
}
----------------------------------------- ----------------------
我收到一个错误,说objNode没有设置为XmlNode。我通过代码
,但肯定不是 - 为什么?
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?



Shakir Hussain< sh ********* **@hotmail.com>写道:
Shakir Hussain <sh***********@hotmail.com> wrote:
foreach循环是一个美丽的概念,可以节省许多头痛和不必要的变量分配。这是迭代
集合的最快方法。但是,请记住以下内容

1.阅读时无法从集合中删除值
2.阅读时无法添加值
3.无法刷新值在阅读时

要解决你的问题,请执行此操作。

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

foreach(XmlNode objNode)在nList)
{
System.WriteLine(objNode.Value.ToString());
}
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());
}




那不应该'' t以任何方式影响发布的代码,但是,

发布的代码没有改变收藏。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



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


这篇关于foreach循环混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆