Problem using default xml namespace and selectsignlenode/selectnod

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rrm

    Problem using default xml namespace and selectsignlenode/selectnod

    Hi

    I am verifying a SLD implementation using xml, however I have som problem
    using xpath and default namespaces in vb.net

    Currently I have the following xml
    <?xml version="1.0"?>
    <StyledLayerDes criptor version="1.0.0"
    xsi:schemaLocat ion="http://www.opengis.net/sld
    http://schemas.opengis .net/sld/1.0.0/StyledLayerDesc riptor.xsd"
    xmlns:sld="http ://www.opengis.net/sld" xmlns:ogc="http ://www.opengis.net/ogc"
    xmlns:xlink="ht tp://www.w3.org/1999/xlink"
    xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
    <Name>TestNam e</Name>
    <Title>TestTitl e</Title>
    <Abstract>TestA bstract</Abstract>
    </StyledLayerDesc riptor>

    and the following code to read it:
    Dim writer As Xml.XmlTextWrit er = New Xml.XmlTextWrit er(file, Nothing)
    doc.Load(file)
    where file is the location of the xml and doc is a xmldocument objekt

    I ask with:
    Dim r As Xml.XmlNode = doc.SelectSingl eNode("/StyledLayerDesc riptor/Name")
    Console.WriteLi ne("Name " & r.InnerXml)

    The return value is nothing, any clue of why??? If I dont use default
    namespace, then it can be found.

    rrm
  • Martin Honnen

    #2
    Re: Problem using default xml namespace and selectsignlenod e/selectnod



    rrm wrote:

    Dim r As Xml.XmlNode = doc.SelectSingl eNode("/StyledLayerDesc riptor/Name")
    The return value is nothing, any clue of why??? If I dont use default
    namespace, then it can be found.
    You need a namespace manager e.g.
    Dim namespaceManage r As XmlNamespaceMan ager =_
    new XmlNamespaceMan ager(doc.NameTa ble)
    namespaceManage r.AddNamespace( "pf", "default namespace URI here")
    then e.g.
    doc.SelectSingl eNode("/pf:StyledLayerD escriptor/pf:Name",_
    namespaceManage r)

    --

    Martin Honnen --- MVP XML

    Comment

    • rrm

      #3
      Re: Problem using default xml namespace and selectsignlenod e/selec


      Hi
      You need a namespace manager e.g.
      Dim namespaceManage r As XmlNamespaceMan ager =_
      new XmlNamespaceMan ager(doc.NameTa ble)
      namespaceManage r.AddNamespace( "pf", "default namespace URI here")
      then e.g.
      doc.SelectSingl eNode("/pf:StyledLayerD escriptor/pf:Name",_
      namespaceManage r)
      ok

      I found an error in my posting: xmlns:sld=... should be xmlns=...
      <StyledLayerDes criptor version="1.0.0"
      xsi:schemaLocat ion="http://www.opengis.net/sld
      http://schemas.opengis .net/sld/1.0.0/StyledLayerDesc riptor.xsd"
      xmlns="http://www.opengis.net/sld" xmlns:ogc="http ://www.opengis.net/ogc"
      xmlns:xlink="ht tp://www.w3.org/1999/xlink"
      xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">

      Would that help me to avoid using namespace "ns:" in selectsinglenod e???

      Are there any different in the way dotnet 1 and dotnet 2 handles this??

      Comment

      • Martin Honnen

        #4
        Re: Problem using default xml namespace and selectsignlenod e/selec



        rrm wrote:

        I found an error in my posting: xmlns:sld=... should be xmlns=...
        Would that help me to avoid using namespace "ns:" in selectsinglenod e???
        You need to use a namespace manager if you want to select elements or
        attributes in a namespace. An XPath (1.0) expression in the form of e.g.
        element-name
        only selects elements of that name in _no_ namespace. If your elements
        are in a namespace then you need a namespace manager that binds a prefix
        to the namespace URI and then you need to use that prefix in the XPath
        expression e.g.
        pf:element-name

        See also "Namespaces in XPath Expressions" in
        <http://msdn2.microsoft .com/en-us/library/d271ytdx.aspx>


        --

        Martin Honnen --- MVP XML

        Comment

        Working...