OpenXml: Convert an XElement to an OpenXmlElement

前端 未结 1 1337
我在风中等你
我在风中等你 2021-01-13 06:06

How would I go about converting an XElement to an OpenXmlElement? Either my google-fu fails or this has not been addressed.

相关标签:
1条回答
  • 2021-01-13 07:01

    You can convert a given OpenXmlElement to a XElement using the following code:

    OpenXmlElement el = ...; // Code to get the xml element from your office doc.
    
    // Then use XElement.Parse and the OuterXml property.
    XElement xel = XElement.Parse(el.OuterXml);
    

    To convert an XElement to an OpenXmlElement try the following code:

    XElement xe = ...;
    using(StreamWriter sw = new StreamWriter(new MemoryStream()))
    {
      sw.Write(xe.ToString());
      sw.Flush();
      sw.BaseStream.Seek(0, SeekOrigin.Begin);
    
      OpenXmlReader re = OpenXmlReader.Create(sw.BaseStream);
    
      re.Read();
      OpenXmlElement oxe = re.LoadCurrentElement();
      re.Close();
    }
    

    Hope, this helps.

    0 讨论(0)
提交回复
热议问题