none
Retrieving the text for a given style RRS feed

  • Question

  •  

    Apologies for asking what should be a simple question...

     

    I want to use the OpenXml SDK to retrieve the "normal" text that is related to a particular style. For example, if I have a document that contains several "Heading1" Styles, I'd like to iterate through the Xml and return all of the text that is formatted in Heading1. Using the following code, I can obtain all of the styles in the document, but how can I obtain the actual text associated with each one?

     

    Code Block
    private void btnXml_Click(object sender, EventArgs e)
            {
                XmlDocument requirementsDoc = new XmlDocument();
               
                string Styles = null;
               
                try
                {
                    using (WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\Users\ted.malone\Documents\StyleTest.docx", false))
                    {
                        //Use the main document part
                        MainDocumentPart main = doc.MainDocumentPart;
                        requirementsDoc.Load(main.GetStream());
                        //Search for all style elements
                         XmlNodeList nodeList = requirementsDoc.GetElementsByTagName("w:pStyle");
                        //Use an enumerator to iterate through the nodeList
                         IEnumerator counter = nodeList.GetEnumerator();
                         while (counter.MoveNext())
                         {
                             XmlNode Requirement = (XmlNode)counter.Current;
                             Styles = Styles + Requirement.OuterXml.ToString();
                         }
                        
                    }
                    MessageBox.Show(Styles.ToString(),"Style Elements",MessageBoxButtons.OK);
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Exception", MessageBoxButtons.OK);
                }
            }

     

     

    I would be grateful for any pointers, thanks!
    Thursday, December 6, 2007 7:48 PM

Answers

  • All text formatted with a paragraph style called 'mystyle':

     

    //wStick out tongue[wStick out tonguePr/wStick out tongueStyle/@w:val='mystyle']/w:r/w:t

     

    All runs formatted with a character style called mystyle:

     

    //wStick out tongue/w:r[w:rPr/w:rStyle/@w:val='mystyle']/w:t

     

    Hope it helps,

     

    Wouter

    Monday, December 17, 2007 11:54 AM

All replies

  • The code below gets all the text from a collection of nodes. For example, your code could find the collection of nodes with Heading1 style. Is that what you wanted to do?

     

    John D.

     

    Code Block

    StringBuilder sb = new StringBuilder();

    XmlNodeList texts = _element.GetElementsByTagName("w:t");

    foreach(XmlNode text in texts)

    {

    sb.Append(text.InnerText);

    }

    return sb.ToString();

     

     

    Monday, December 10, 2007 10:23 AM
  • Thanks John;

     

    The issue with this code is that it retrieves all text elements, not just those text elements associated with "Heading 1" styles..

     

    This does help me move in the correct direction though..

     

    Thanks.

     

    Ted

     

    Wednesday, December 12, 2007 9:24 PM
  • All text formatted with a paragraph style called 'mystyle':

     

    //wStick out tongue[wStick out tonguePr/wStick out tongueStyle/@w:val='mystyle']/w:r/w:t

     

    All runs formatted with a character style called mystyle:

     

    //wStick out tongue/w:r[w:rPr/w:rStyle/@w:val='mystyle']/w:t

     

    Hope it helps,

     

    Wouter

    Monday, December 17, 2007 11:54 AM
  • Thank you, that was exactly what I was looking for.

     

    Wednesday, January 9, 2008 2:23 AM