MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

XPath and XmlNodeList, the horror, the horror!

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp
Author Message
DotNetNewbie



Joined: 11 Dec 2007
Posts: 7

PostPosted: Wed Dec 12, 2007 8:06 pm    Post subject: XPath and XmlNodeList, the horror, the horror! Reply with quote

Thanks all for the previous help btw.

What is going on here? It works yet it doesn't want to work in a
loop.



XmlNodeList nodeList = root.SelectNodes(String.Format("/cars/
mfg[@name='{0}'/model]", folderName));


This works:

nodeList[0].ChildNodes[0].Attributes["name"].Value.ToString()

nodeList[0].ChildNodes[1].Attributes["name"].Value.ToString());


but if I try and loop through them I get an error: Expression must
evaluate to a node-set.

my loop:

for (int x = 0; x < nodeList[0].ChildNodes.Count; x++)
{
XmlNode node = nodeList[0].ChildNodes[x];

url = new UrlPattern();

url.Name = node.Attributes["name"].Value.ToString();
url.RegexString = node.Attributes["regexString"].Value.ToString();
url.RealPath = node.Attributes["realPath"].Value.ToString();
url.Format = node.Attributes["format"].Value.ToString();


rgUrlPatterns.Add(url);
}

Archived from group: microsoft>public>dotnet>languages>csharp
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 266

PostPosted: Thu Dec 13, 2007 4:17 am    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

DotNetNewbie wrote:



> but if I try and loop through them I get an error: Expression must
> evaluate to a node-set.

When? At compile-time, or execution time?

Where? Which line of code?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.


--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
DotNetNewbie



Joined: 11 Dec 2007
Posts: 7

PostPosted: Thu Dec 13, 2007 12:26 am    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

On Dec 12, 6:17 pm, Jon Skeet [C# MVP] wrote:
> DotNetNewbie wrote:
>
>
>
> > but if I try and loop through them I get an error: Expression must
> > evaluate to a node-set.
>
> When? At compile-time, or execution time?
>
> Where? Which line of code?
>
> Could you post a short but complete program which demonstrates the
> problem?
>
> Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfor details of
> what I mean by that.
>
> --
> Jon Skeet - http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

This line reports the error:

for (int x = 0; x < nodeList[0].ChildNodes.Count; x++)


I'll post the working code next....
Back to top
View user's profile Send private message
Marc Gravell



Joined: 08 Aug 2007
Posts: 20

PostPosted: Thu Dec 13, 2007 2:25 am    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

Well, what does the xpath mean?

XmlNodeList nodeList = root.SelectNodes(String.Format("/cars/
mfg[@name='{0}'/model]", folderName));

Support folderName is "Foo", then this gives xpath of

"/cars/mfg[@name='Foo'/model]"

I suggest that you simply need to lose the single quotes.

Marc
Back to top
View user's profile Send private message
Marc Gravell



Joined: 08 Aug 2007
Posts: 20

PostPosted: Thu Dec 13, 2007 2:26 am    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

typo: Support = Suppose

Stupid fingers, be less sloppy!
Back to top
View user's profile Send private message
DotNetNewbie



Joined: 11 Dec 2007
Posts: 7

PostPosted: Thu Dec 13, 2007 10:40 am    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

On Dec 12, 6:17 pm, Jon Skeet [C# MVP] wrote:
> DotNetNewbie wrote:
>
>
>
> > but if I try and loop through them I get an error: Expression must
> > evaluate to a node-set.
>
> When? At compile-time, or execution time?
>
> Where? Which line of code?
>
> Could you post a short but complete program which demonstrates the
> problem?
>
> Seehttp://www.pobox.com/~skeet/csharp/complete.htmlfor details of
> what I mean by that.
>
> --
> Jon Skeet - http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk


Jon,

My XML:















My codebehind:

protected void Page_Load(object sender, EventArgs e)
{


string pathToFile = String.Format("{0}/cars.xml",

HttpContext.Current.Request.PhysicalApplicationPath);


XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(pathToFile);

XmlNode root = xmlDoc.DocumentElement;

string mfgName = "honda";

XmlNodeList nodeList = root.SelectNodes(String.Format("/
cars/car[@mfg='{0}'/model]", mfgName));


//Response.Write("nodeList: " + (null == nodeList));
//Response.Write("nodelist2: " +
nodeList[0].ChildNodes[0].Attributes["name"].Value.ToString());
//Response.Write("nodelist2: " +
nodeList[0].ChildNodes[1].Attributes["name"].Value.ToString());
//Response.End();



for (int x = 0; x < nodeList[0].ChildNodes.Count; x++)
{
XmlNode node = nodeList[0].ChildNodes[x];

Response.Write("forloop name = " +
node.Attributes["name"].Value.ToString());


}

}
Back to top
View user's profile Send private message
Hans Kesting



Joined: 02 Oct 2007
Posts: 3

PostPosted: Thu Dec 13, 2007 8:15 pm    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

DotNetNewbie submitted this idea :
> Thanks all for the previous help btw.
>
> What is going on here? It works yet it doesn't want to work in a
> loop.
>
>
>
> XmlNodeList nodeList = root.SelectNodes(String.Format("/cars/
> mfg[@name='{0}'/model]", folderName));
>

Shouldn't that XPath be
"/cars/mfg[@name='{0}']/model"
(moved the ] bracket)
or is that a typo in the post?

Hans Kesting
Back to top
View user's profile Send private message
Martin Honnen



Joined: 08 Aug 2007
Posts: 70

PostPosted: Thu Dec 13, 2007 8:29 pm    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

DotNetNewbie wrote:

>
>
>
>
>
>
>
>
>
>
>
>
>
> My codebehind:
>
> protected void Page_Load(object sender, EventArgs e)
> {
>
>
> string pathToFile = String.Format("{0}/cars.xml",
>
> HttpContext.Current.Request.PhysicalApplicationPath);
>
>
> XmlDocument xmlDoc = new XmlDocument();
>
> xmlDoc.Load(pathToFile);
>
> XmlNode root = xmlDoc.DocumentElement;
>
> string mfgName = "honda";
>
> XmlNodeList nodeList = root.SelectNodes(String.Format("/
> cars/car[@mfg='{0}'/model]", mfgName));

Looks to me like you want
XmlNodeList modelList = xmlDoc.SelectNodes(string.Format(
"/cars/car[@mfg = '{0}']/model", mfgName));
and then simply
foreach (XmlElement model in modelList)
{
Response.Write(model.GetAttribute("name") + "");
}



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 266

PostPosted: Fri Dec 14, 2007 12:17 am    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

DotNetNewbie wrote:



> My codebehind:

Now would be a very good time to learn how to write a short but
complete program, rather than presenting codebehind snippets.

See http://pobox.com/~skeet/csharp/incomplete.html

You'll find it a *lot* quicker to debug problems like this (which have
nothing to do with ASP.NET per se) when you've got them in a small test
harness.

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
DotNetNewbie



Joined: 11 Dec 2007
Posts: 7

PostPosted: Thu Dec 13, 2007 6:48 pm    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

On Dec 13, 2:17 pm, Jon Skeet [C# MVP] wrote:
> DotNetNewbie wrote:
>
>
>
> > My codebehind:
>
> Now would be a very good time to learn how to write a short but
> complete program, rather than presenting codebehind snippets.
>
> Seehttp://pobox.com/~skeet/csharp/incomplete.html
>
> You'll find it a *lot* quicker to debug problems like this (which have
> nothing to do with ASP.NET per se) when you've got them in a small test
> harness.
>
> --
> Jon Skeet - http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

Ok the code works fine in a Console application, but on the web I am
getting an error.

My console code (xml file: http://pastebin.com/m550db746 )


using System;
using System.Xml;

class Program
{
static void Main(string[] args)
{

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(@"E:\Test\cars.xml");
string mfgName = "honda";

XmlNodeList modelList = xmlDoc.SelectNodes(string.Format("/
cars/car[@mfg = '{0}']/model", mfgName));

foreach (XmlElement model in modelList)
{
Console.WriteLine(model.GetAttribute("name"));
}

Console.ReadLine();


}
}

I used the exact same code for my web page, but I replaced the
console.writes with response.writes, and loading the XML by URL.

My code is here:

protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(Server.MapPath("/cars.xml"));
string mfgName = "honda";

XmlNodeList modelList = xmlDoc.SelectNodes(string.Format("/
cars/car[@mfg = '{0}']/model", mfgName));

foreach (XmlElement model in modelList)
{
Response.Write("Name: " +
model.GetAttribute("name"));
}
}

(same XML file).

Again the error on the webpage is:

Exception Details: System.Xml.XPath.XPathException: Expression must
evaluate to a node-set.
Source Error:
foreach (XmlElement page in nodeList)
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 266

PostPosted: Fri Dec 14, 2007 4:12 am    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

DotNetNewbie wrote:



> Again the error on the webpage is:
>
> Exception Details: System.Xml.XPath.XPathException: Expression must
> evaluate to a node-set.
> Source Error:
> foreach (XmlElement page in nodeList)

Has it definitely load the file correctly? What does it look like in
the debugger?

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
Martin Honnen



Joined: 08 Aug 2007
Posts: 70

PostPosted: Fri Dec 14, 2007 7:30 pm    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

DotNetNewbie wrote:

> I used the exact same code for my web page, but I replaced the
> console.writes with response.writes, and loading the XML by URL.
>
> My code is here:
>
> protected void Page_Load(object sender, EventArgs e)
> {
> XmlDocument xmlDoc = new XmlDocument();
>
> xmlDoc.Load(Server.MapPath("/cars.xml"));
> string mfgName = "honda";
>
> XmlNodeList modelList = xmlDoc.SelectNodes(string.Format("/
> cars/car[@mfg = '{0}']/model", mfgName));
>
> foreach (XmlElement model in modelList)
> {
> Response.Write("Name: " +
> model.GetAttribute("name"));
> }
> }
>
> (same XML file).
>
> Again the error on the webpage is:
>
> Exception Details: System.Xml.XPath.XPathException: Expression must
> evaluate to a node-set.
> Source Error:
> foreach (XmlElement page in nodeList)

But your code above has
foreach (XmlElement model in modelList)
so why would the error message say
foreach (XmlElement page in nodeList)
? That does not make sense to me. It looks like the error is elsewhere,
not in the code you provided.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Back to top
View user's profile Send private message
DotNetNewbie



Joined: 11 Dec 2007
Posts: 7

PostPosted: Fri Dec 14, 2007 1:43 pm    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

On Dec 13, 6:12 pm, Jon Skeet [C# MVP] wrote:
> DotNetNewbie wrote:
>
>
>
> > Again the error on the webpage is:
>
> > Exception Details: System.Xml.XPath.XPathException: Expression must
> > evaluate to a node-set.
> > Source Error:
> > foreach (XmlElement page in nodeList)
>
> Has it definitely load the file correctly? What does it look like in
> the debugger?
>
> --
> Jon Skeet - http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

yes the file is loaded correctly, I put debug tag when it loads the
file, and during the loop.

After it loads the file and hits the forloop, in vs.net I can view
that the cars.xml file has loaded just fine.
Back to top
View user's profile Send private message
DotNetNewbie



Joined: 11 Dec 2007
Posts: 7

PostPosted: Fri Dec 14, 2007 8:23 pm    Post subject: Re: XPath and XmlNodeList, the horror, the horror! Reply with quote

On Dec 14, 11:43 am, DotNetNewbie wrote:
> On Dec 13, 6:12 pm, Jon Skeet [C# MVP] wrote:
>
>
>
> > DotNetNewbie wrote:
>
> >
>
> > > Again the error on the webpage is:
>
> > > Exception Details: System.Xml.XPath.XPathException: Expression must
> > > evaluate to a node-set.
> > > Source Error:
> > > foreach (XmlElement page in nodeList)
>
> > Has it definitely load the file correctly? What does it look like in
> > the debugger?
>
> > --
> > Jon Skeet - http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> > World class .NET training in the UK:http://iterativetraining.co.uk
>
> yes the file is loaded correctly, I put debug tag when it loads the
> file, and during the loop.
>
> After it loads the file and hits the forloop, in vs.net I can view
> that the cars.xml file has loaded just fine.

Ok got it to work, I was referencing the child nodes incorrectly!
thanks for all your replies!

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
xpath question with namespace Hi; If I have the xml:
XPath to node with empty value Hi all, Basically i want to do get the specific node that is empty but when i try the following i get a null node even though I know there is a node whose AN value is null. What am i doing wrong? Than

Binding to XPath with prefix in GridViewColumn? I have a ListView with an ItemSource that is bound to some XML, via {Binding Xpath=...}. The ListView.View is defined with a GridView. In the individual elements, I'd like to specify and bind to the various XML eleme

XPath: Multiple conditions (contains, dates, node-set) Hi all, I am using the evaluate function on .net (xpath 1 right?). Now I need to expand the code to do multiple contains, compare dates (such as 'before', 'between' and 'after'), and so on. This is the thought: 'geo

Using XPath to read VS 2005 csproj files I am loading a VS2005 csproj file into an XPathDocument then am creating an I am trying to use XPath to pull the Include attributes from the Element of the ItemGroup Element. For some reason I cannot get the XPath syntax
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group