2

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>
CarlGanz
  • 189
  • 11
  • What have You tried? Have You got any idea how to proceed with such thing? Give it a try and post it in the question. – Tatranskymedved Feb 09 '17 at 14:54
  • I tried loading into Xdocument, XmlTextReader, and XslTransform but no luck. It looks like you could get a list of variables using XslTransform like this: xslt._QueryStore[0].CompiledQuery but _QueryStore is not recognized outside the Immediate Window. – CarlGanz Feb 09 '17 at 15:05
  • There is no use of parameters nor variables in your XSLT snippet, the samples `` and `` have an XPath expression that selects an element node. A variable or parameter would be referenced as `` but then there would need to be an `xsl:param name="varName"` or `xsl:variable name="varName"` in the code. – Martin Honnen Feb 09 '17 at 15:24

1 Answers1

2

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

Community
  • 1
  • 1
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
  • Many thanks for this. I was hoping there might be a Framework object that handled this but it looks like I'll need to make do with XML parsing. – CarlGanz Feb 09 '17 at 15:16
  • The very last link is pointing to "how to load" XML document into object. There is high chance that there exist some method which can do the work for You on that object, just look it up. =) – Tatranskymedved Feb 09 '17 at 15:24