Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C#, ASP .Net and XPath question

Status
Not open for further replies.

bbolte

Programmer
Sep 30, 2002
113
US
I'm trying to get the value of one node in an xml doc as such:

XPathDocument xmlCont = new XPathDocument(Server.MapPath("assets/xml/" +
PageDisplay));
XPathNavigator ContentTitle = xmlCont.CreateNavigator();
ContentTitle.MoveToRoot();
ContentTitle.MoveToFirstChild();
ContentTitle.MoveNext();
PageTitle = ContentTitle.Value;

The xml looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<PageContent>
<h1>The Page Title</h1>
<image>
<path>image.jpg</path>
<id>xxx</id>
<alt>xxx</alt>
<width>xxx</width>
<height>xx</height>
</image>
...
</PageContent>


But PageTitle seems to have every value from every node of the whole xml
document in it. I've tried different things (NodeIterator, calling the
specific node name, etc) but I keep getting the same result. What have I
missed?
 
You can't do an XPath like:
[tab]PageContent/h1[1]
to get the first occurance of the header tags?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I'm not following your response. Can you explain a little bit more what you're trying to say?
 
Not sure if you have solved this or not, if so please post the code that fixed this problem.

I think the problem you are running into is that you are not traversing the XPath correctly.

When you use MoveToNext it tries to go to the next same level node of <PageContent>, I am guessing that you want to go to the next element, Which happens to be a child of <PageContent>.

You should use MoveToFirstChild. Since you are currently on <PageContent> the first child node is <h1>. So then you can get the value of it which I am assuming is the PageTitle you are after.

Code:
      XPathDocument xmlCont = new XPathDocument(PageDisplay);
      XPathNavigator ContentTitle = xmlCont.CreateNavigator();
      ContentTitle.MoveToRoot(); //moves to <?xml
      ContentTitle.MoveToFirstChild();//moves to <PageContent
      //ContentTitle.MoveToNext();//tries to go to the node after </PageContent
      ContentTitle.MoveToFirstChild();//moves to <h1
      PageTitle = ContentTitle.Value;//get value inside <h1

let us know how it turns out.

Randall2nd
 
actually, i never got back to this. spent most of the time trying to get a valid xhtml output. i'll give this a go this weekend. thanks a lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top