none
Remove or ignore the xmlns:xsi when processing an xml file in .net

    Question

  • HELLO! I am a newbie when it comes to XML file processing. What I am trying to do is step through an XML and store the elements in varaibles that I can place in a SQL Database. I can successfully do that when the xmlns is not included in the first node of the xml. Example:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <monthlydata xmlns="http://something.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

     

    If the i take out the xmlns and the xmlns:xsi so it reads:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <monthlydata>

     

    Then it works fine.

     

    Is there a way that I can ignore the xmlns and the xmlns:xsi or remove it while i am processing the xml. I want to do this because I do not want to use it. PLEASE HELP! Below is the code that I am using to process the xml:

     

     

    'Create the XML Document

    XDoc = New XmlDocument()

    'Load the Xml file

    XDoc.Load(fileName)

    'Get the list of monthlydata nodes

    MonthlydataList = XDoc.SelectNodes("/monthlydata")

     

    For each XMLnode in Monthlydatalist

     

    SOME CODE HERE

     

    Next

    Tuesday, August 28, 2007 2:21 AM

Answers

  • You should neither remove nor ignore the namespace declarations.  Instead, you should understand what they're doing, and what you can do to work with them.

     

    http://www.w3schools.com/xml/xml_namespaces.asp is a good start for understanding what namespaces are for and how they work. 

     

    I say "start" because it doesn't clarify a point that confuses everyone who tries to make sense of namespaces:  the prefixes are just a shortcut, meaningful only within the XML element that they're declared in.  Outside that element - in another XML document, say, or in your programs - the only thing that's significant is the namespace URI.

     

    Whenever you're using XPath, you have to give the XPath processor a way of mapping the prefixes that you're going to use in your patterns with the namespace URIs in the document.  This is done via the XmlNamespaceManager in .NET:

     

    Code Snippet

    XmlNamespaceManager nsm = new XmlNamespaceManager(new XmlNameTable());

    nsm.AddNamespace("x", "http://something.org");

    nsm.AddNamespace("y", "http://www.w3.org/2001/XMLSchema-instance");

     

    This creates a new XmlNamespaceManager, and tells it that the "x" prefix maps to one namespace, and the "y" prefix maps to another.

     

    Now you can find nodes in the first namespace, as long as you give the XmlNamespaceManager to the SelectNodes method so that it can tell what you mean by "x":

     

    Code Snippet
    foreach (XmlNode n in doc.SelectNodes("/x:monthlydata", nsm))

     

    If you're document contains an XML Schema, you can process it too:

     

    Code Snippet
    foreach (XmlNode n in doc.SelectNodes("/x:monthlydata/y:schema/y:element", nsm))

     

    will iterate through all of the top-level element declarations in your document's schema, if there's an inline schema.

     

    The only time it's meaningful to use XPath without prefixes is when the nodes you're searching for are in no namespace.  There aren't a lot of nontrivial applications of XML where this is reliably going to be the case.

    Tuesday, August 28, 2007 5:55 AM
  • Robert is right.

     

    Do read:

     

      http://sinan.ussakli.net/code/xml-namespaces-on-xpath-queries-and-xslt

    or

      http://www.topxml.com/people/bosley/defaultns.asp

     

    Hope this helps.

     

    Cheers,

    Dimitre Novatchev

     

    Wednesday, August 29, 2007 3:55 AM

All replies

  • You should neither remove nor ignore the namespace declarations.  Instead, you should understand what they're doing, and what you can do to work with them.

     

    http://www.w3schools.com/xml/xml_namespaces.asp is a good start for understanding what namespaces are for and how they work. 

     

    I say "start" because it doesn't clarify a point that confuses everyone who tries to make sense of namespaces:  the prefixes are just a shortcut, meaningful only within the XML element that they're declared in.  Outside that element - in another XML document, say, or in your programs - the only thing that's significant is the namespace URI.

     

    Whenever you're using XPath, you have to give the XPath processor a way of mapping the prefixes that you're going to use in your patterns with the namespace URIs in the document.  This is done via the XmlNamespaceManager in .NET:

     

    Code Snippet

    XmlNamespaceManager nsm = new XmlNamespaceManager(new XmlNameTable());

    nsm.AddNamespace("x", "http://something.org");

    nsm.AddNamespace("y", "http://www.w3.org/2001/XMLSchema-instance");

     

    This creates a new XmlNamespaceManager, and tells it that the "x" prefix maps to one namespace, and the "y" prefix maps to another.

     

    Now you can find nodes in the first namespace, as long as you give the XmlNamespaceManager to the SelectNodes method so that it can tell what you mean by "x":

     

    Code Snippet
    foreach (XmlNode n in doc.SelectNodes("/x:monthlydata", nsm))

     

    If you're document contains an XML Schema, you can process it too:

     

    Code Snippet
    foreach (XmlNode n in doc.SelectNodes("/x:monthlydata/y:schema/y:element", nsm))

     

    will iterate through all of the top-level element declarations in your document's schema, if there's an inline schema.

     

    The only time it's meaningful to use XPath without prefixes is when the nodes you're searching for are in no namespace.  There aren't a lot of nontrivial applications of XML where this is reliably going to be the case.

    Tuesday, August 28, 2007 5:55 AM
  • I am still confused. If I read that article right really the only purpose for namespaces is to be able to tell the difference between elements with the same name. In my case there will be no elements with the same name. I just want to know if there is any way that I can ignore the namespace.

    Tuesday, August 28, 2007 8:33 PM
  • You could use this hack:

    MonthlydataList = XDoc.SelectNodes("/*[local-name() = monthlydata]")
    Tuesday, August 28, 2007 8:50 PM
  • How do you know there will be no elements with the same name?  How much control do you have over the parts of your document that aren't in your target namespace?

    Wednesday, August 29, 2007 12:12 AM
  • I have all the control that I want. The XML's will be uploaded to a server where i can do with them whatever I want. I just have to extract the data from them, store in variables and pass to SQL Server. I have done this before and I wanted to use the same process only before there was no namespace.

    Wednesday, August 29, 2007 12:39 AM
  • Robert is right.

     

    Do read:

     

      http://sinan.ussakli.net/code/xml-namespaces-on-xpath-queries-and-xslt

    or

      http://www.topxml.com/people/bosley/defaultns.asp

     

    Hope this helps.

     

    Cheers,

    Dimitre Novatchev

     

    Wednesday, August 29, 2007 3:55 AM
  • If you have control over the format of the XML, just take all the namespace-qualified elements out of the document.  Then you don't have to worry about them at all.

     

    I suspect, however, that you don't have complete control over the XML.  If you did, you wouldn't have this problem.  You have this problem because some other process, some tool or external system or whatever, is adding elements in another namespace to a document that you have to process.

     

    The nature of namespaces that aren't yours is that you don't have complete control over them.  I couldn't give you a list of all the legal element names in XML Schema if you put a gun to my head.  I can't claim that I'm never going to use an element named "annotation" or "import" in one of my XML documents that also includes a schema.  The day I do, XPath queries that get around namespaces via constructs like "local-name() = 'annotation'" are going to start picking up elements in other namespaces besides the one I do have control over.

     

    Namespaces are a pain in the butt.  (And the rocket scientist at the W3C who came up with the idea of using URLs for namespace URIs deserves a stern talking-to.)  But they're an important tool.  They accomplish something that really can't be accomplished in any better way.  They're not going away.  So it's important to understand the if you want to build software that continues to work when its input changes.

    Wednesday, August 29, 2007 7:56 AM
  •  

    You would think that if the XML document were completely valid, you could use something like:

    string xpath = @"/gsml:GSML/gsml:member/gsml:LithodemicUnit/gml:name";

    XmlNamespaceManager xnsMgr = new XmlNamespaceManager(xdoc.NameTable);

    System.Xml.XmlNodeList xnodes = xdoc.SelectNodes(xpath, xnsMgr);

    foreach (XmlNode xn in xnodes)

    .

    .

    .

     

    i.e. get the NameTable from the instance document, but apparently this doesn't work (getting an error that namespace 'gml' is not defined, but it is in the document and the document validates)...

    Friday, October 26, 2007 6:38 PM
  •  

    I've got a situation where there is an xmlns="http://...." on an element.

     

    The xml file is a web.config file and the xmlns serves only to help Visual Studio provide intelisense.

     

    I am trying to provide a simple MsBuild task for modifying this file in a build process. The end user must not be obliged to deal with the complexity of namespacing in order to use it.

     

    The xml namespaces are irrelevant.

     

    A large number of responses I have found follow the same patterrn:

    "You should neither remove nor ignore the namespace declarations.  Instead, you should understand what they're doing, and what you can do to work with them."

     

    These kind of response are not very helpfull

     

     

    For lack of a better solution I'll probably modify the user's Xpath (input arguament) to use [local-path()=...]

     

     

    Thursday, July 17, 2008 4:35 PM
  • "You should neither remove nor ignore the namespace declarations.  Instead, you should understand what they're doing, and what you can do to work with them."

     

    This is true in 99.8% cases. If you really need move all elements to a different namespace (including the empty namespace) and you understand what the consequences are you can do the following (Xslt):

     

    Code Snippet

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

     

    <xsl:template match="*">

    <xsl:element name="{local-name()}">

    <xsl:copy-of select="@*"/>

    <xsl:apply-templates />

    </xsl:element>

    </xsl:template>

     

    </xsl:stylesheet>

     

     

     

    This piece of code will move *ALL* elements to the empty namespace regardless of the original namespace they were in. (note that it won't copy comments and processing-instructions to the ouput document but this is easy to fix if you need it)

     

    Pawel

     

     

    Thursday, July 17, 2008 5:39 PM
    Moderator
  •  

    I don't really want to change the whole document, just ignore a couple namespaces that are irrelevant.

     

    I've settled on using another task before and after my current tasks.

     

    The new task opens the file and puts a couple underscore in front of the undesired xmlns attributes, then I run my normal tasks, then I run a task that removes the underscores.

     

    Its a kluge but it will get the job done

     

    Thursday, July 17, 2008 7:23 PM
  • Namespaces are not just a decoration to an element. Changing/removing/ignoring/etc. namespace affects all nodes that belong to this namespace. This means that if you want to do it right you often need to consider the whole document.

    Certainly you can hack it by treating the Xml as a Text (as you did) but you risk that the output will no longer be a valid Xml document (e.g. did you consider prefixed elements? what would happen if someone adds the namespace again and you already prefixed one with underscore - you probably will end with two attributes having the same name -> Invalid Xml document etc.).

     

    Pawel

     

    Thursday, July 17, 2008 9:33 PM
    Moderator
  •  

    Of course I consider the whole document, thats why I don't transform the whole document. I specify the namespaces to bypass and I restore them afterwards.

     

    These xmlns attributes are used by Visual Studio to provide intellisense in a config file. The sub elements do not use any prefixes.

    Friday, July 18, 2008 1:39 PM