• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Processing Instructions in XML

I need to read in the Processing instruction from an XML file and place it in a variable...

XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<doc>
    .....................................................................
</doc>

I need to read the href attribute out of the xml-stylesheet element.
0
JHalstead
Asked:
JHalstead
  • 2
  • 2
1 Solution
 
drichardsCommented:
If this is in a file, then here's one way to get it:

      System.Xml.XmlTextReader rdr = new System.Xml.XmlTextReader("<filename>");
      while ( rdr.Read() )
      {
            if ( rdr.NodeType == System.Xml.XmlNodeType.ProcessingInstruction )
            {
                  // rdr.Value has 'type="text/xsl" href="test.xsl"' - parse out what you need
                  // rdr.Name has 'xml-stylesheet'
            }
      }
0
 
drichardsCommented:
Oops, that was C# code, here's the VB:

        Dim rdr As System.Xml.XmlTextReader = New System.Xml.XmlTextReader("..\\..\\..\\CSConsole\\XmlFile1.xml")
        While (rdr.Read())
            If (rdr.NodeType = System.Xml.XmlNodeType.ProcessingInstruction) Then
                System.Console.WriteLine(rdr.Name + " " + rdr.Value)
            End If
        End While
0
 
JHalsteadAuthor Commented:
It's works great, thanks, one question though.  rdr.Value returnes both the HREF and the TYPE, I would liek to return just the HREF.  I was thinking of creating an XML node from this returned info and selecting the HREF attribute, but I thought you might know an easier way.
0
 
JHalsteadAuthor Commented:
Ok, never mind I got that part, it was easier than I though.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now