OpenXml: Convert an XElement to an OpenXmlElement

喜欢而已 提交于 2019-12-07 05:28:40

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8419008/openxml-convert-an-xelement-to-an-openxmlelement

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