0

Currently, I am unable to access the content of any of the comments in the XSD I am attempting to read from.

Here is a snippet of the XSD I am trying to read comments from:

<xs:schema>
    <xs:complexType name="typeName">
        <xs:choice>
            <xs:element name="elementName" type="someOtherType"><!--This is a comment I would like to access.--></xs:element>
        </xs:choice>
    </xs:complexType>
</xs:schema>

I happen to be using VB.Net. Here is a snippet of the class that is attempting to access the XML comment from the above XSD:

Public Class QAutoTestXSD

    Private XML As MSXML2.DOMDocument60

    Public Sub New()
        XML = New MSXML2.DOMDocument60
        Call XML.setProperty("SelectionNamespaces", "xmlns:xs='http://www.w3.org/2001/XMLSchema'")
        XML.async = False
        If Not XML.load("C:\myXML.xml") Then
            Call handleParsingError
        End If
    End Sub

    Public Function getXSDComment(typeName As String, elementName As String) As String
        getXSDComment = XML.selectSingleNode("/xs:schema/xs:complexType[@name='" & typeName & "TestType']/xs:choice/xs:element[@name='" & elementName & "']/comment()").nodeValue
    End Function

End Class

By modifying the XPATH argument of selectSingleNode, I am able to successfully access all other node types in the XSD, however I have yet to successfully select any nodes of comment type, even using "//comment()". Any help would be much appreciated!

ssalbdivad
  • 26
  • 8
  • possible duplicate of [Accessing Comments in XML using XPath](http://stackoverflow.com/questions/784745/accessing-comments-in-xml-using-xpath) – Tony Hopkinson Jun 23 '14 at 16:36
  • 1
    The `loadXML` method takes a string with XML markup, not a file name. And why are you using MSXML with .NET, System.Xml.XmlDocument or System.Xml.Linq.XDocument are pure managed implementations. Also `//comments()` should give an XPath error, the node test is singular `//comment()`. – Martin Honnen Jun 23 '14 at 16:48
  • @MartinHonnen As I'm sure you can tell, all of the code is just a mock up. I apologize for the two errors you pointed out, I will correct them, however these issues were not present in the original code. As far as MSXML, I am translating legacy VBA to VB.Net, and would like to maintain as much of the old code base as possible to expedite the transition. – ssalbdivad Jun 23 '14 at 16:57
  • @TonyHopkinson I am aware of this question, however the accepted answer is identical to the method I've been trying unsuccessfully to use. – ssalbdivad Jun 23 '14 at 17:02

1 Answers1

1

Consider to post a minimal but complete sample allowing us to reproduce the problem. When I use

Module Module1

    Sub Main()
        Dim doc As MSXML2.DOMDocument60 = New MSXML2.DOMDocument60
        If doc.load("XMLFile1.xml") Then
            doc.setProperty("SelectionNamespaces", "xmlns:xs='http://www.w3.org/2001/XMLSchema'")
            Dim comment As MSXML2.IXMLDOMComment = doc.selectSingleNode("//xs:element//comment()")
            If comment IsNot Nothing Then
                Console.WriteLine(comment.nodeValue)
            End If
        End If
    End Sub

End Module

where XMLFile1.xml is

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="typeName">
    <xs:choice>
      <xs:element name="elementName" type="someOtherType">
        <!--This is a comment I would like to access.-->
      </xs:element>
    </xs:choice>
  </xs:complexType>
</xs:schema>

then the program (compiled with VS 2012 as a .NET 4.5 application) outputs "This is a comment I would like to access.".

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • 1
    Thanks Martin, this works fine. The problem ended up having to do with my VS resources not updating properly, so totally external to what was posted. I feel at this point my question is essentially irrelevant, would it be best for me to simply delete it? – ssalbdivad Jun 23 '14 at 17:39
  • 1
    Yes, you can delete it. – Martin Honnen Jun 23 '14 at 17:49