Convert outerXml to OpenXmlElement

2.1k views Asked by At

I know how to convert an OpenXmlElement to outer xml by using the OpenXmlElement.OuterXml function but is it possible to do the opposite without knowing what kind of OpenXmlElement it's going to be?

For example I have the outerXml of a table, paragraph and run. Then is it possible to do something like:

new OpenXmlElement(unknownXml)

I saw the constructor of the OpenXmlElement is kept protected.

I'm not asking for any dirty switch statements or something like that.

---------------- Edit ------------------

I have come up with my own solution. I've created a library that does the conversion without using a dirty switch statement :-)

The library can be found here: https://github.com/LucBos/OpenXmlFactory

The usage of the library is described here: http://www.lucbos.net/2012/01/reconstructing-openxml-element-from-xml.html

1

There are 1 answers

0
emd On

You can make a generic OpenXmlElement by reading a stream from an XElement into a new OpenXmlElement. However, afik there isn't a clean way to get the appropriate type.