Beispiel #1
0
 internal override void WriteContentTo(XmlWriter w)
 {
     // Write the loaded inner xml if there are any
     if (ShadowElement is not null)
     {
         ShadowElement.WriteContentTo(w);
     }
 }
Beispiel #2
0
 /// <inheritdoc/>
 internal override void WriteContentTo(XmlWriter w)
 {
     // Write the loaded inner xml if there are any
     if (ShadowElement != null)
     {
         ShadowElement.WriteContentTo(w);
     }
     else
     {
         // nothing to write
         w.WriteString(Text);
     }
 }
Beispiel #3
0
        private protected override void Populate(XmlReader xmlReader, OpenXmlLoadMode loadMode)
        {
            LoadAttributes(xmlReader);

            if (!xmlReader.IsEmptyElement)
            {                     // only when element is not empty (not  <element />).
                xmlReader.Read(); // read this element

                RawInnerText = string.Empty;

                int         unwanted         = 0;
                int         textNodePosition = -1; // the position of the text in the ShadowElement's children when there are other unexpected node.
                XmlNodeType textNodeType     = XmlNodeType.Text;

                if (xmlReader.NodeType == XmlNodeType.EndElement)
                {
                    Debug.Assert(xmlReader.LocalName.Equals(LocalName));
                }
                else
                {
                    while (!xmlReader.EOF)
                    {
                        if (xmlReader.NodeType == XmlNodeType.EndElement)
                        {
                            Debug.Assert(xmlReader.LocalName.Equals(LocalName));
                            break;
                        }
                        else if (string.IsNullOrEmpty(RawInnerText) &&
                                 (xmlReader.NodeType == XmlNodeType.Text ||
                                  xmlReader.NodeType == XmlNodeType.CDATA ||
                                  xmlReader.NodeType == XmlNodeType.SignificantWhitespace ||
                                  xmlReader.NodeType == XmlNodeType.Whitespace /* O15:#3024890 */))
                        {
                            // text or CDATA
                            // scenario: normal text element <Application>Microsoft Office Word</Application>
                            // scenario: <w:t>This is <![CDATA[Xml Example <tag>text</tag>.]]> 1</w:t>

                            // only load text when no text is loaded,
                            // for case "<foo/>Text1<bar/>Text2", only load "Text1", very rare case
                            RawInnerText     = xmlReader.Value;
                            textNodePosition = unwanted;
                            textNodeType     = xmlReader.NodeType;

                            xmlReader.Read();
                        }
                        else
                        {
                            Debug.Assert(xmlReader.NodeType != XmlNodeType.EntityReference);

                            // Load unexpected children if there are any.
                            OpenXmlElement child = ElementFactory(xmlReader);
                            child.Load(xmlReader, OpenXmlLoadMode.Full);
                            unwanted++;

                            if (ShadowElement == null)
                            {
                                ShadowElement = new OpenXmlUnknownElement(Prefix, LocalName, NamespaceUri);
                            }

                            ShadowElement.AppendChild(child);
                        }
                    }
                }

                if (unwanted == 0)
                {
                    // only text node, no unwanted children
                    Debug.Assert(ShadowElement == null);
                }
                else if (textNodePosition > -1)
                {
                    // place an OpenXmlMiscNode for the loaded text in the ShadowElement so that we can write out correct content in serialization.
                    OpenXmlMiscNode textNode = null;
                    switch (textNodeType)
                    {
                    case XmlNodeType.Text:
                        textNode = OpenXmlMiscNode.CreateFromText(RawInnerText);
                        break;

                    case XmlNodeType.CDATA:
                        textNode = OpenXmlMiscNode.CreateFromCdata(RawInnerText);
                        break;

                    case XmlNodeType.SignificantWhitespace:
                    case XmlNodeType.Whitespace:     /* O15:#3024890 */
                        textNode = OpenXmlMiscNode.CreateFromSignificantWhitespace(RawInnerText);
                        break;
                    }

                    ShadowElement.InsertAt(textNode, textNodePosition);
                }
                else
                {
                    // textNodePosition == -1, no text loaded.
                }
            }

            xmlReader.Skip(); // skip the end tag

            // set raw outer xml to empty to indicate that it is passed
            RawOuterXml = string.Empty;
        }