OpenXml: Convert an XElement to an OpenXmlElement

感情迁移 提交于 2019-12-05 11:08:11

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!