none
Open XML document without schema RRS feed

  • Question

  • I have been using XDocument.Load to process an type of XML document, Chemical Markup Language (CML). It happens that a very popular program that saves to CML files, does not add the schema xmlns="http://www.xml-cml.org/schema"  to the <molecule> root element.

    The XML file looks like:

    <molecule>
     <atomArray>
      <atom id="a1" elementType="H" x3="-1.470636" y3="-0.595643" z3="0.187903"/>
    ...

    And using the code

    Dim xdoc = XDocument.Load(filename) Dim mol = xdoc.<molecule>

            Dim atomarray = mol.<atomArray>.First

    The variable mol ends up with some information but not enumerated. The next line atomarray throws an error.

    However if the <molecule> tag looks like

    <molecule xmlns="http://www.xml-cml.org/schema">
    

    The rest of the code reads the file correctly.

    So I am wondering if I could read the XML file without having to provide the link to schema. In fact a lot of examples in MSDN don't appear to have the xmlns attribute. Maybe I have missed something important. Thanks for the help



    Sunday, September 10, 2017 11:14 AM

Answers

  • If you have an Imports statement related to Xml namespace and it is not needed in your current code file, then remove it.

    Otherwise you have to declare the second Imports having an empty value:

    Imports <xmlns:ns="">

    then specify the namespace in your code when you work with non-conforming files:

    Dim mol = xdoc.<ns:molecule>
    Dim atomarray = mol.<ns:atomArray>.First

    However, this makes problematic the usage of the same code for both of the file formats. Maybe consider fixing the files that do not have the namespace.


    There are some alternative ways, like using XmlDocument instead of XDocument and its SelectNodes that takes an XmlNamespaceManager.

    • Edited by Viorel_MVP Sunday, September 10, 2017 4:22 PM
    • Marked as answer by LMAPerdigao Monday, September 11, 2017 8:55 AM
    Sunday, September 10, 2017 4:01 PM

All replies

  • I have been using XDocument.Load to process an type of XML document, Chemical Markup Language (CML). It happens that a very popular program that saves to CML files, does not add the schema xmlns="http://www.xml-cml.org/schema"  to the <molecule> root element.

    The XML file looks like:

    <molecule>
     <atomArray>
      <atom id="a1" elementType="H" x3="-1.470636" y3="-0.595643" z3="0.187903"/>
    ...

    And using the code

    Dim xdoc = XDocument.Load(filename) Dim mol = xdoc.<molecule>

            Dim atomarray = mol.<atomArray>.First

    The variable mol ends up with some information but not enumerated. The next line atomarray throws an error.

    However if the <molecule> tag looks like

    <molecule xmlns="http://www.xml-cml.org/schema">

    The rest of the code reads the file correctly.

    So I am wondering if I could read the XML file without having to provide the link to schema. In fact a lot of examples in MSDN don't appear to have the xmlns attribute. Maybe I have missed something important. Thanks for the help



    I suspect that there's more to this than what's being shown, but maybe the following might help some. I'm using LINQ-To-XML to get the data:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) Handles MyBase.Load
    
            Dim xDoc As XElement = _
                <molecule>
                    <atomArray>
                        <atom id="a1" elementType="H" x3="-1.470636" y3="-0.595643" z3="0.187903"/>
                    </atomArray>
                </molecule>
    
            For Each atomInfo As XElement In xDoc...<atom>
                Dim id As String = atomInfo.@id
                Dim elementType As String = atomInfo.@elementType
                Dim x3 As Double = CDbl(atomInfo.@x3)
                Dim y3 As Double = CDbl(atomInfo.@y3)
                Dim z3 As Double = CDbl(atomInfo.@z3)
    
                Stop
    
                ' Hover your mouse over each of
                ' the variables above and you'll
                ' see the respective values.
            Next
    
            Stop
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, September 10, 2017 12:03 PM
  • If you have an Imports statement related to Xml namespace and it is not needed in your current code file, then remove it.

    Otherwise you have to declare the second Imports having an empty value:

    Imports <xmlns:ns="">

    then specify the namespace in your code when you work with non-conforming files:

    Dim mol = xdoc.<ns:molecule>
    Dim atomarray = mol.<ns:atomArray>.First

    However, this makes problematic the usage of the same code for both of the file formats. Maybe consider fixing the files that do not have the namespace.


    There are some alternative ways, like using XmlDocument instead of XDocument and its SelectNodes that takes an XmlNamespaceManager.

    • Edited by Viorel_MVP Sunday, September 10, 2017 4:22 PM
    • Marked as answer by LMAPerdigao Monday, September 11, 2017 8:55 AM
    Sunday, September 10, 2017 4:01 PM
  • Thank you. Yes, In my class I had defined

    Imports <xmlns="http://www.xml-cml.org/schema">
    Which after commenting, the code worked correctly

    Monday, September 11, 2017 8:45 AM
  • You code works and I like the alternative. It turns out that I had an imports statement in my code defining the xml namespace (see below). Only after removing it, the program will successfully continue. I didn't know I could read the xml file without the import statement.
    Monday, September 11, 2017 8:54 AM