question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposed OpenXmlReader methods

See original GitHub issue

In order to make more succinct calling code, I’ve extended OpenXmlReader in my code with these helpers. I propose that something similar can be added to the library.

public T LoadCurrentElement<T>() where T : OpenXmlElement => (T)_reader.LoadCurrentElement();

public bool ElementTypeIs<T>() where T : OpenXmlElement => ElementType == typeof(T);

public bool ElementIs<T>(out T element) where T : OpenXmlElement
{
       element = null;

        if (ElementTypeIs<T>())
        {
            element = LoadCurrentElement<T>();
            return true;
       }

       return false;
}

ElementIs<T> plays nice with the new out semantics introduced in C# 7.0. Instead of this mess,

if (reader.ElementType == typeof(Paragraph))
{
    Paragraph p = (Paragraph)reader.LoadCurrentElement();
    // ...
}

the new methods provide a simplification

if (reader.ElementIs(out Paragraph p))
{
    // ...
}

Thoughts?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:25 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
igiturcommented, Feb 8, 2018

I’m just a spectator here, but it’s definitely something we could use in ClosedXML. Here’s a snippet of our current code:

else if (reader.ElementType == typeof(AutoFilter))
    LoadAutoFilter((AutoFilter)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(SheetProtection))
    LoadSheetProtection((SheetProtection)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(DataValidations))
    LoadDataValidations((DataValidations)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(ConditionalFormatting))
    LoadConditionalFormatting((ConditionalFormatting)reader.LoadCurrentElement(), ws, differentialFormats);
else if (reader.ElementType == typeof(Hyperlinks))
    LoadHyperlinks((Hyperlinks)reader.LoadCurrentElement(), wsPart, ws);
else if (reader.ElementType == typeof(PrintOptions))
    LoadPrintOptions((PrintOptions)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(PageMargins))
    LoadPageMargins((PageMargins)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(PageSetup))
    LoadPageSetup((PageSetup)reader.LoadCurrentElement(), ws, pageSetupProperties);
else if (reader.ElementType == typeof(HeaderFooter))
    LoadHeaderFooter((HeaderFooter)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(SheetProperties))
    LoadSheetProperties((SheetProperties)reader.LoadCurrentElement(), ws, out pageSetupProperties);
else if (reader.ElementType == typeof(RowBreaks))
    LoadRowBreaks((RowBreaks)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(ColumnBreaks))
    LoadColumnBreaks((ColumnBreaks)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(WorksheetExtensionList))
    LoadExtensions((WorksheetExtensionList)reader.LoadCurrentElement(), ws);
else if (reader.ElementType == typeof(LegacyDrawing))
    ws.LegacyDrawingId = (reader.LoadCurrentElement() as LegacyDrawing).Id.Value;

Looks like it is an ideal candidate for use of the proposed new methods.

0reactions
github-actions[bot]commented, May 15, 2020

Stale issue message

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proposed OpenXmlReader methods · Issue #398
I propose that something similar can be added to the library. public T LoadCurrentElement<T>() where T : OpenXmlElement => (T)_reader.
Read more >
OpenXmlReader Class (DocumentFormat.OpenXml)
Initializes a new instance of the OpenXmlReader class using the supplied Boolean value. Properties. Attributes. Gets the list of attributes of the current ......
Read more >
c# - Using OpenXmlReader
I am trying to read (large) Excel 2007+ spreadsheets, and Google has kindly informed me that using the OpenXml SDK is a pretty...
Read more >
Template Based Approach to Export Data to Excel: Part III
In this article you will learn how to work with a template based approach to export data to Excel.
Read more >
Write Large Excel Files Using the Open XML SDK.mp4
Comments7 · Reading Large Excel Files using the Open XML SDK · How to create Excel files with C# code · Using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found