Adding <?mso-application ...> programmatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Victor Hadianto

    Adding <?mso-application ...> programmatically

    Hi,

    If I have an XmlDocument DOM how do I insert
    <?mso-application progid="ProgId. Here"?> programmaticall y?


    --
    Victor Hadianto


  • Derek Harmon

    #2
    Re: Adding &lt;?mso-application ...&gt; programmaticall y

    "Victor Hadianto" <synop@nospam.n ospam> wrote in message news:06AEA580-5F3B-4283-8D74-76EB75F61697@mi crosoft.com...[color=blue]
    > If I have an XmlDocument DOM how do I insert
    > <?mso-application progid="ProgId. Here"?> programmaticall y?[/color]

    Given an already positioned XmlNode, you can call the XmlDocument's
    CreateProcessin gInstruction( ) and use InsertBefore( ), InsertAfter( )
    or AppendChild( ) to place the P.I. in relation to the XmlNode. e.g.,

    string progId = "ProgId.Her e";

    xmlDoc.InsertBe fore(
    xmlDoc.CreatePr ocessingInstruc tion(
    "mso-application",
    string.Format( "progid=\"{0}\" ", progId)
    ),
    xmlNodeToInsert Before
    );

    The first 'token' after the <? is called the target of the P.I. and is the first
    argument to CreateProcessin gInstruction( ), any remaining text after the
    target up until the ?> is the second argument. The second argument may
    contain multiple 'pseudo-attributes'.


    Derek Harmon


    Comment

    • Victor Hadianto

      #3
      Re: Adding &lt;?mso-application ...&gt; programmaticall y

      > Given an already positioned XmlNode, you can call the XmlDocument's[color=blue]
      > CreateProcessin gInstruction( ) and use InsertBefore( ), InsertAfter( )
      > or AppendChild( ) to place the P.I. in relation to the XmlNode. e.g.,
      >
      > string progId = "ProgId.Her e";
      >
      > xmlDoc.InsertBe fore(
      > xmlDoc.CreatePr ocessingInstruc tion(
      > "mso-application",
      > string.Format( "progid=\"{0}\" ", progId)
      > ),
      > xmlNodeToInsert Before
      > );
      >
      > The first 'token' after the <? is called the target of the P.I. and is the first
      > argument to CreateProcessin gInstruction( ), any remaining text after the
      > target up until the ?> is the second argument. The second argument may
      > contain multiple 'pseudo-attributes'.[/color]

      Thanks,

      Is there a way (without using Regex) to find out if I already have the <?mso
      ....?> on the document?

      ---
      Victor Hadianto

      Comment

      • Oleg Tkachenko [MVP]

        #4
        Re: Adding &lt;?mso-application ...&gt; programmaticall y

        Victor Hadianto wrote:
        [color=blue]
        > Is there a way (without using Regex) to find out if I already have the <?mso
        > ...?> on the document?[/color]

        XmlNode pi =
        xmlDoc.SelectSi ngleNode("/processing-instruction('ms o-application')") ;
        if (pi != null)

        --
        Oleg Tkachenko [XML MVP]

        Comment

        • Derek Harmon

          #5
          Re: Adding &lt;?mso-application ...&gt; programmaticall y

          "Victor Hadianto" <synop@nospam.n ospam> wrote in message news:29545D69-FB25-4B5E-8B93-6C242714A0DC@mi crosoft.com...[color=blue]
          > Is there a way (without using Regex) to find out if I already have the <?mso
          > ...?> on the document?[/color]

          There are at least three ways to find out if there's a processing instruction
          in an XmlDocument that is (or starts with) "mso".

          1. The method Oleg already posted is to use the XPath function,
          processing-instruction( ). I'll add that if you want to find all PIs
          starting with "mso", you'd use the following XPath expression,

          //processing-instruction()[starts-with(local-name(),'mso')]

          because the local-name() of a PI in XPath corresponds to the
          PI's target.

          2. Enumerate the DOM nodes of the document and when you
          find a node with a NodeType of XmlNodeType.Pro cessingInstruct ion
          then you can examine it's LocalName and Value properties to get it's
          target and pseudo-attributes, respectively.

          - - - PiDomSearch.cs (excerpt)
          // . . .
          public void Descend( XmlNode node)
          {
          if ( null != node )
          {
          if ( XmlNodeType.Pro cessingInstruct ion == node.NodeType )
          {
          if ( node.LocalName. StartsWith( "mso"))
          {
          // ... do something here ...
          }
          }
          this.Descend( node.FirstChild );
          this.Descend( node.NextSiblin g);
          }
          return;
          }
          // . . .
          this.Descend( xmlDoc.Document Element);
          // . . .
          - - -


          3. The prior techniques may be appealing if you're already traversing
          the DOM node tree of an XmlDocument, but if you want the information
          a priori then I think the best method is to use a specialized XmlReader
          when loading the XmlDocument that would be responsible for detecting
          the presence of these processing instructions,

          - - - PiReaderSearch. cs (excerpt)
          using System;
          using System.Collecti ons;
          using System.IO;
          using System.Xml;
          // . . .

          /// <summary>
          /// <b>XmlTextReade r</b> subclass that detects the presence of processing-instructions
          /// as it reads an incoming <b>TextReader </b> whose targets start with a given text string.
          /// </summary>
          public class ProcInstXmlText Reader : XmlTextReader
          {
          private string piTarget;
          private ArrayList matches;
          private bool listening;

          public ProcInstXmlText Reader( TextReader reader, string piTarget) : base( reader)
          {
          this.piTarget = piTarget;
          this.matches = new ArrayList( );
          }

          public int MatchCount
          {
          get
          {
          return matches.Count;
          }
          }

          public override XmlNodeType NodeType
          {
          get
          {
          this.intercepti ng = ( XmlNodeType.Pro cessingInstruct ion == base.NodeType );
          return base.NodeType;
          }
          }

          public override string Name
          {
          get
          {
          string tgt = base.Name;
          if ( this.intercepti ng )
          {
          if ( tgt.StartsWith( this.piTarget))
          {
          this.matches.Ad d( tgt);
          this.intercepti ng = false;
          }
          }
          return tgt;
          }
          }
          }
          // . . .

          ProcInstXmlText Reader pixtr = new ProcInstTextRea der(
          new StreamReader( "filename.xml") , "mso"
          );
          xmlDoc.Load( pixtr);
          if ( pixtr.MatchesCo unt > 0 )
          {
          // Process xmlDoc knowing that mso* PIs are present.
          }
          else
          {
          // Process xmlDoc knowing that it contains no mso* PIs.
          }

          // . . .
          - - -

          These examples presume you're looking for processing instructions that
          start with the leading characters, "mso" (working with Microsoft Office
          XML), but they're also easily adapted to testing for equality to "mso" if
          you're looking for only one specific processing instruction target.


          Derek Harmon


          Comment

          Working...