all groups > dotnet xml > june 2005 >
You're in the

dotnet xml

group:

XML in VS.Net 2K5 vs VS.Net 2K3


Re: XML in VS.Net 2K5 vs VS.Net 2K3 Martin Honnen
6/28/2005 12:00:00 AM
dotnet xml:

[quoted text, click to view]

If you are processing elements in a namespace then in XPath (1.0) you
need a prefix bound to that namespace e.g.
descendant::xs:element[@ref='CurrDate']




--

Martin Honnen --- MVP XML
XML in VS.Net 2K5 vs VS.Net 2K3 SideByEach
6/28/2005 6:43:28 AM
I just started using VS 2K5 beta 2 the other day. I am trying to move a
project I created over to in pieces to 2K5.
I can't seem to figure out why the follow XPath returns nothing in 2K5
"descendant::element[@ref='CurrDate']".

The XML I am trying to select from is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="CurrDate" type="xs:string"/>

<xs:element name="NextGenReport">
<xs:complexType>
<xs:sequence>
<xs:element ref="CurrDate"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

I am selecting from the node "NextGenReport". I have a namespace
manager created and the "xs" namespace has been added.

Sample code:

Dim nt As NameTable = New NameTable()
Dim _nsMgr as New XmlNamespaceManager(nt)

Dim nav As XPath.XPathNavigator = schemaNode.CreateNavigator

If nav.MoveToFirstNamespace() Then
Do While nav.Name <> String.Empty
_nsMgr.AddNamespace(nav.Name, nav.Value)

If Not
nav.MoveToNextNamespace(XPath.XPathNamespaceScope.ExcludeXml) Then Exit
Do
Loop 'nav.Name <> String.Empty
End If 'nav.MoveToFirstNamespace()

<some code omitted>

Dim testSelect As XmlNodeList
testSelect =
otherNode.SelectNodes(descendant::element[@ref='CurrDate'], _nsMgr)

I can't really see what I am doing wrong. Any help would be appreciated.
Re: XML in VS.Net 2K5 vs VS.Net 2K3 SideByEach
6/28/2005 7:02:02 AM
I kind of found answer to my problem. I changed my XPath to look like
this "descendant::node()[local-name()='element' and @ref='CurrDate']".

I'm still not sure why the old works in one and not the other. What's
the point of using the namespace manager anymore?
Re: XML in VS.Net 2K5 vs VS.Net 2K3 Chris Lovett
6/28/2005 11:43:39 PM
The following works on my machine:

using System;
using System.Xml;

namespace ConsoleApplication1 {
public class Program {

static void Main(string[] args) {
XmlDocument doc = new XmlDocument();
doc.Load("..\\..\\test.xml");

XmlNamespaceManager mgr = new
XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
string query = "descendant::xs:element[@ref='CurrDate']";

XmlNode node = doc.SelectSingleNode(query, mgr);

Console.WriteLine(node.OuterXml);
}
}
}




[quoted text, click to view]

Re: XML in VS.Net 2K5 vs VS.Net 2K3 SideByEach
7/4/2005 9:55:12 AM
With your solution Chris there would be no way to select nodes with the
same name but in different namespaces.
Re: XML in VS.Net 2K5 vs VS.Net 2K3 Chris Lovett
7/4/2005 11:36:42 PM
That was not clear from your initial request, if you really do want to
select based on local name and not on namespace then the earlier answer was
the correct one:

"descendant::node()[local-name()='element' and @ref='CurrDate']".


[quoted text, click to view]

AddThis Social Bookmark Button