CarlGanz
CarlGanz

Reputation: 197

Retrieve parameters in XSL file using C#

In the snippet below, I'm trying to use C# to retrieve the value-of select variables. I want to open a series of XSL files and obtain a list of these field names dynamically like this:

txtAdditionalInfo

txtFormattedDate

so the user can add their own values to be merged with the XSL. How can I retrieve these names?

Thanks

Carl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Document">
    <html>
      <head>
      </head>
      <body>
        <table>
          <tr>
            <td width="75"></td>
            <td>
              <table width="660" border="0">
                  <tr>
                    <td colspan="2" style='font-size:12.0pt;font-family:"Arial";text-align:left'>
<xsl:value-of select="txtAdditionalInfo"/> 
                      <br /><br />
                      <br />
                    </td>
                  </tr>

                  <tr>
                    <td align="left" height="32" valign="bottom">
                    </td>
                    <td align="left" height="32" valign="bottom" style='font-size:12.0pt;font-family:"Arial";text-align:left'>
<xsl:value-of select="txtFormattedDate"/>
                    </td>

Upvotes: 2

Views: 1770

Answers (1)

Tatranskymedved
Tatranskymedved

Reputation: 4371

Just to obtain the text (easy part of task) You can it 3 ways (even more):

  1. Read each line & parse the ones which contains "xsl:value-of":

This one is pretty easy do-able just by such code like:

string[] lines = System.IO.File.ReadAllLines("File.xls");
foreach(string line in lines)
{
    if((line).Trim().StartsWith("<xls:value-of"))
    {
        Console.WriteLine(line.Split(new[]{" select=\"", "\"/>"},
                      StringSplitOptions.RemoveEmptyEntries)[0]);
    }
}

This should write out all the "values" if the <xsl:value-of will be on separate line. You can adjust somehow the code above to get the expected result if they are not.

  1. Use regex to parse the document & to find the exact line:

This one requires some knowledge about Regex (regual expressions). You can try online example also to do this:

string xls = System.IO.File.ReadAllText("File.xls");
string pattern = @"<xsl:value-of\s+select="(\w+)"\s*\/>";
var lResult = Regex.Match(xls, pattern);

if(lResult.Success)
    foreach( var iGroup in lResult.Groups)
        Console.WriteLine(iGroup);

Online example at: https://regex101.com/r/uViRnx/1 (I haven't tested in real code, might require little adjustments).

  1. Parse whole file as a XML file:

Well this parts is the hardest, but might give You the biggest advantage in future, it You would like to modify the file further. You should load up the XLS document as XML file in either XLS object or Your own object -> this process is called Deserialization.

There is a topic on XML deserialization: How to Deserialize XML document

There is a topic how to read the XLS doc: https://www.codeproject.com/Tips/801032/Csharp-How-To-Read-xlsx-Excel-File-With-Lines-of

Upvotes: 2

Related Questions