Processing Instructions in XML

Posted on 2004-11-04
Last Modified: 2013-11-19
I need to read in the Processing instruction from an XML file and place it in a variable...


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

I need to read the href attribute out of the xml-stylesheet element.
Question by:JHalstead
    LVL 19

    Expert Comment

    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'
    LVL 19

    Accepted Solution

    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

    Author Comment

    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.

    Author Comment

    Ok, never mind I got that part, it was easier than I though.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now