none
get assigned prefix

    Question

  • Hi all;

    One more question (we have a really good testing team).

    XPathNavigator.Prefix is the prefix for a node as it is listed in the XML file. How can I get the prefix I gave it when I assigedned the namespaces when opening my XmlDocument the XPathNavigator comes from?

    I have to do this to handle the case of a the same prefix being used for 2 different namespaces (some of our customers do this).

    ??? - thanks - dave


    Very funny video - What's your Metaphor?
    Tuesday, November 23, 2010 10:02 PM

Answers

  • I think that complicated-looking XPath expression in your first line of code has scared everyone off :)

    Really, I haven't tried to figure out that piece of code either, but if what you want to know is what prefix has been assigned per an XmlNamespaceManager:

    1. Take the NamespaceURI of your XmlNode.

    2. Get your XmlNamespaceManager object (you probably have to carry it around).

    3. Call XmlNamespaceManager.LookupPrefix passing in the URI from step #1.

    http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.lookupprefix.aspx

    If this does not help, then I'd recommend that you write a complete, self-contained example (as short as possible) that demonstrates the problem with a comment when you get to the part you can't figure out so that someone can try things out and actually run it easily.  You'll most likely get an answer if you do this!

     

     

    • Marked as answer by DavidThi808 Thursday, December 09, 2010 11:54 PM
    Thursday, December 09, 2010 11:43 PM

All replies

  • Use NamespaceURI, not Prefix.  This will identify the namespace regardless of the prefix.  Also, this will give you something that you can cross reference elsewhere -- i.e., with the XmlNamespaceManager that you have been using.

     

     

    Wednesday, November 24, 2010 12:00 AM
  • Hi;

    I am doing that where I can. But in this case I have a UI that is listing out the schema to be used for XPath statements and we need to show the user the prefix, not the namespace. I've assigned the namespaces to the XmlDocument so is there a way to query it with a namespace to get the prefix I assigned and not the prefix used in the document?

    This is only an issue where the same prefix is used for multiple namespaces in a document.

    thanks - dave


    Very funny video - What's your Metaphor?
    Wednesday, November 24, 2010 9:54 PM
  • > get the prefix I assigned

    Could you show me the code where you assigned this prefix, and roughly how you put your XPath together.  It might be possible to ask the object on which you made the assignment to give you the "reverse".

     

    Wednesday, November 24, 2010 11:18 PM
  • Hi BC;

    First off, if you're in the U.S., Happy Thanksgiving.

    Here's the code where I set it:

    		private void SetNamespace(XmlDocument doc)
    		{
    
    			string str = WindwardWrapper.getProperty("xpath.default.prefix");
    			if (!string.IsNullOrEmpty(str))
    				defaultPrefix = str;
    
    			XmlNodeList list = doc.SelectNodes("//namespace::*[name() != 'xml'][not(../../namespace::*=.)]");
    			if (list != null)
    				foreach (XmlNode nodeOn in list)
    				{
    					XmlAttribute attr = nodeOn as XmlAttribute;
    					if ((attr == null) || (attr.LocalName == "xsi"))
    						continue;
    					if (context == null)
    						context = new XmlNamespaceManager(doc.NameTable);
    					context.AddNamespace(string.IsNullOrEmpty(attr.LocalName) || attr.LocalName == "xmlns" ? defaultPrefix : attr.LocalName , attr.Value);
    				}
    		}
    
    

    And then in a very seperate part of the code I have a XPathNavigator that represents a select at some point in the document. I use XPathNavigator.Name for the full name of that element.

    I have access to the underlying XmlDocument if that helps and XPathNavigator.NamespaceURI has the namespace uri for the element. If there is no other way, I could create and pass down a Dictionary of URIs to prefixs assigned. But I'd prefer to avoid that.

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, November 25, 2010 10:52 PM
  • What is it exactly that you want to achieve? Do you want to query the XmlNamespaceManager for the prefix? There is a method LookupPrefix that allows that:

      context.LookupPrefix(navigator.NamespaceURI)


    MVP Data Platform Development My blog
    Friday, November 26, 2010 12:52 PM
  • That doesn't work - however this could be due to what I am doing. Here's my code for setting up the namespaces:

    // call this when first starting.
    private void SetNamespace(XmlDocument doc)
    {
    
    	XmlNodeList list = doc.SelectNodes("//namespace::*[name() != 'xml'][not(../../namespace::*=.)]");
    	if (list != null)
    		foreach (XmlNode nodeOn in list)
    		{
    			XmlAttribute attr = nodeOn as XmlAttribute;
    			if ((attr == null) || (attr.LocalName == "xsi"))
    				continue;
    			if (context == null)
    				context = new XmlNamespaceManager(doc.NameTable);
    			string prefix = string.IsNullOrEmpty(attr.LocalName) || attr.LocalName == "xmlns" ? defaultPrefix : attr.LocalName;
    			context.AddNamespace(prefix, attr.Value);
    			namespaceMap.Add(attr.Value, prefix);
    		}
    }
    
    // call this later when I've renamed prefixes for cases where the same prefix is mapped to multiple URLs
    public void SetNamespaces(XPathNamespace[] namespaces)
    {
    	context = new XmlNamespaceManager(xmlDocument.NameTable);
    	namespaceMap.Clear();
    	foreach (XPathNamespace nsOn in namespaces)
    	{
    		context.AddNamespace(nsOn.Prefix, nsOn.Url);
    		namespaceMap.Add(nsOn.Url, nsOn.Prefix);
    	}
    }
    
    

    As I am pulling from the XmlDocument.NameTable when creating the context the second time, is it retaining my initial settings and therefore ignoring any changes?

    If so, how do I reset the context namespaces?

    thanks - dave


    Very funny video - What's your Metaphor?
    Friday, November 26, 2010 6:02 PM
  • Hi all;

    Asking again, is there a way to get the prefix I assigned to a namespace from a node?

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, December 09, 2010 4:41 PM
  • I think that complicated-looking XPath expression in your first line of code has scared everyone off :)

    Really, I haven't tried to figure out that piece of code either, but if what you want to know is what prefix has been assigned per an XmlNamespaceManager:

    1. Take the NamespaceURI of your XmlNode.

    2. Get your XmlNamespaceManager object (you probably have to carry it around).

    3. Call XmlNamespaceManager.LookupPrefix passing in the URI from step #1.

    http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.lookupprefix.aspx

    If this does not help, then I'd recommend that you write a complete, self-contained example (as short as possible) that demonstrates the problem with a comment when you get to the part you can't figure out so that someone can try things out and actually run it easily.  You'll most likely get an answer if you do this!

     

     

    • Marked as answer by DavidThi808 Thursday, December 09, 2010 11:54 PM
    Thursday, December 09, 2010 11:43 PM
  • thank you - hadn't thought of that (should have, but didn't).
    Very funny video - What's your Metaphor?
    Thursday, December 09, 2010 11:55 PM