WriteEntityRef() public method

public WriteEntityRef ( string name ) : void
name string
return void
 /// <summary>
 /// This removes namespaces from an XML Element (Scott)
 /// </summary>
 /// <param name="xeElement"></param>
 /// <returns></returns>
 protected string RemoveNamespaces(XmlNode xeElement)
 {
     StringWriter swOutput = new System.IO.StringWriter();
     XmlTextWriter xtwWriter = new XmlTextWriter(swOutput);
     //xtwWriter.WriteStartDocument();
     XmlNodeReader xnrReader = new XmlNodeReader(xeElement);
     while (xnrReader.Read())
     {
         switch (xnrReader.NodeType)
         {
             case XmlNodeType.Element:
                 xtwWriter.WriteStartElement(xnrReader.Name);
                 if (xnrReader.HasAttributes)
                 {
                     while (xnrReader.MoveToNextAttribute())
                     {
                         if (xnrReader.Name != "xmlns")
                         {
                             xtwWriter.WriteAttributeString(xnrReader.Name, xnrReader.Value);
                         }
                     }
                     xnrReader.MoveToElement();
                 }
                 if (xnrReader.IsEmptyElement)
                 {
                     xtwWriter.WriteEndElement();
                 }
                 break;
             case XmlNodeType.Text:
                 xtwWriter.WriteString(xnrReader.Value);
                 break;
             case XmlNodeType.CDATA:
                 xtwWriter.WriteCData(xnrReader.Value);
                 break;
             case XmlNodeType.ProcessingInstruction:
                 xtwWriter.WriteProcessingInstruction(xnrReader.Name, xnrReader.Value);
                 break;
             case XmlNodeType.Comment:
                 xtwWriter.WriteComment(xnrReader.Value);
                 break;
             case XmlNodeType.EntityReference:
                 xtwWriter.WriteEntityRef(xnrReader.Name);
                 break;
             case XmlNodeType.EndElement:
                 xtwWriter.WriteEndElement();
                 break;
         }
     }
     //xtwWriter.WriteEndDocument();
     xtwWriter.Flush();
     xtwWriter.Close();
     xnrReader.Close();
     string sOutput = swOutput.ToString();
     return sOutput;
 }
        /// <summary>
        /// Idea here is to re-create the XmlTextReader Node and this accounts for 50% of the code.
        /// Original code borrowed from http://www.sdltridionworld.com/community/2011_extensions/parentchangenotifier.aspx
        /// Thanks for the work from Serguei Martchenko - would not be possible without his example!
        /// </summary>
        /// <param name="xReader"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private XmlTextReader PreprocessListItems(XmlTextReader xReader, PipelineContext context)
        {
            TextWriter sWriter = new StringWriter();
            XmlTextWriter xWriter = new XmlTextWriter(sWriter);
            string attrName = "metadataFieldValue";
            string attrValue = "";  // set this to 'fieldValue', for example, to debug and prove it is working

            xReader.MoveToContent();

            using(var tdse = new TDSEWrapper())
            {
                while (!xReader.EOF)
                {
                    switch (xReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            xWriter.WriteStartElement(xReader.Prefix, xReader.LocalName, xReader.NamespaceURI);

                            // add all attributes back  -- always START with this to NOT break the GUI
                            xWriter.WriteAttributes(xReader, false);

                            try
                            {
                                // add my custom attribute
                                if (IsValidItem(xReader))
                                {
                                    string id = xReader.GetAttribute("ID");  // URI
                                    TcmUri uri = new TcmUri(id);

                                    if (uri.ItemType == ItemType.Component)
                                    {
                                        // Get Metadata Value
                                        // 0 seconds with no processing
                                        // 12 seconds for 250 Components with TOM API, Component.MetadataFields...
                                        // 12 seconds for 250 Components with Core Service
                                        // 2 seconds for 250 Components with TOM API and GetXML  (FASTEST!)

                                        // Core Service
                                        //attrValue = GetMetadataValue(fieldName, id);

                                        // TDSE...Fastest with GetXML
                                        Component comp = tdse.TDSE.GetObject(id, Tridion.ContentManager.Interop.TDSDefines.EnumOpenMode.OpenModeView) as Component;
                                        attrValue = GetMetadataValue(comp, "article_number");
                                    }

                                    // add new metadata field attribute
                                    xWriter.WriteAttributeString(attrName, attrValue);
                                    xReader.MoveToElement();
                                }
                            }
                            catch (Exception ex)
                            {
                                Trace.TraceError("EXCEPTION " + ex.Message + ex.ToString() + ex.StackTrace);
                            }

                            if (xReader.IsEmptyElement)
                            {
                                xWriter.WriteEndElement();
                            }
                            break;

                        case XmlNodeType.EndElement:
                            xWriter.WriteEndElement();
                            break;
                        case XmlNodeType.CDATA:
                            // Copy CDATA node  <![CDATA[]]>
                            xWriter.WriteCData(xReader.Value);
                            break;
                        case XmlNodeType.Comment:
                            // Copy comment node <!-- -->
                            xWriter.WriteComment(xReader.Value);
                            break;
                        case XmlNodeType.DocumentType:
                            // Copy XML documenttype
                            xWriter.WriteDocType(xReader.Name, null, null, null);
                            break;
                        case XmlNodeType.EntityReference:
                            xWriter.WriteEntityRef(xReader.Name);
                            break;
                        case XmlNodeType.ProcessingInstruction:
                            xWriter.WriteProcessingInstruction(xReader.Name, xReader.Value);
                            break;
                        case XmlNodeType.SignificantWhitespace:
                            xWriter.WriteWhitespace(xReader.Value);
                            break;
                        case XmlNodeType.Text:
                            xWriter.WriteString(xReader.Value);
                            break;
                        case XmlNodeType.Whitespace:
                            xWriter.WriteWhitespace(xReader.Value);
                            break;
                    }
                    xReader.Read();
                };
            }
            xWriter.Flush();

            xReader = new XmlTextReader(new StringReader(sWriter.ToString()));
            xReader.MoveToContent();
            //-> Write XML of tcm:Item out...
            //   This is where the attribute in the config file is matched.
            // Trace.Write(sWriter.ToString() + Environment.NewLine);
            return xReader;
        }
 // Writes the attribute into the provided XmlWriter.
 private void WriteAttributeValue(XmlTextWriter xtw) {
     string attrName = this.Name;
     while (ReadAttributeValue()) {
         if (this.NodeType == XmlNodeType.EntityReference) {
             xtw.WriteEntityRef(this.Name);
         }
         else {
             xtw.WriteString(this.Value);
         }
     }
     this.MoveToAttribute(attrName);
 }
        private XmlTextReader PreprocessListItems(XmlTextReader xReader, PipelineContext context)
        {
            TextWriter sWriter = new StringWriter();
            XmlTextWriter xWriter = new XmlTextWriter(sWriter);
            string attrName = "metadataFieldValue";  // Must be same as extension config,  'selector="@metadataFieldValue"'
            string attrValue = "fieldValue";  // default here to confirm GUI DataExtender is working - will update value below

            xReader.MoveToContent();

            while (!xReader.EOF)
            {
                switch (xReader.NodeType)
                {
                    case XmlNodeType.Element:
                        xWriter.WriteStartElement(xReader.Prefix, xReader.LocalName, xReader.NamespaceURI);

                        // add all list attributes back  -- always START with this to NOT break the GUI
                        xWriter.WriteAttributes(xReader, false);

                        try
                        {
                            // add my custom attribute
                            if (IsValidItem(xReader))
                            {
                                string id = xReader.GetAttribute("ID");  // URI
                                TcmUri uri = new TcmUri(id);

                                if (uri.ItemType == ItemType.Component)
                                {
                                    // Do Work Here....
                                    attrValue = "your new value";
                                }

                                // add new metadata field attribute
                                xWriter.WriteAttributeString(attrName, attrValue);
                                xReader.MoveToElement();
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError("EXCEPTION " + ex.Message + ex.ToString() + ex.StackTrace);
                        }

                        if (xReader.IsEmptyElement)
                        {
                            xWriter.WriteEndElement();
                        }
                        break;

                    case XmlNodeType.EndElement:
                        xWriter.WriteEndElement();
                        break;
                    case XmlNodeType.CDATA:
                        // Copy CDATA node  <![CDATA[]]>
                        xWriter.WriteCData(xReader.Value);
                        break;
                    case XmlNodeType.Comment:
                        // Copy comment node <!-- -->
                        xWriter.WriteComment(xReader.Value);
                        break;
                    case XmlNodeType.DocumentType:
                        // Copy XML documenttype
                        xWriter.WriteDocType(xReader.Name, null, null, null);
                        break;
                    case XmlNodeType.EntityReference:
                        xWriter.WriteEntityRef(xReader.Name);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        xWriter.WriteProcessingInstruction(xReader.Name, xReader.Value);
                        break;
                    case XmlNodeType.SignificantWhitespace:
                        xWriter.WriteWhitespace(xReader.Value);
                        break;
                    case XmlNodeType.Text:
                        xWriter.WriteString(xReader.Value);
                        break;
                    case XmlNodeType.Whitespace:
                        xWriter.WriteWhitespace(xReader.Value);
                        break;
                }
                xReader.Read();
            };

            xWriter.Flush();

            xReader = new XmlTextReader(new StringReader(sWriter.ToString()));
            xReader.MoveToContent();

            //-> Write XML of tcm:Item out...
            //   This is where the attribute in the config file is matched -> selector="@metadataFieldValue"
            // Trace.Write(sWriter.ToString() + Environment.NewLine);
            return xReader;
        }
 // Writes the current node into the provided XmlWriter.
 private void WriteNode(XmlTextWriter xtw, bool defattr) {
     int d = this.NodeType == XmlNodeType.None ? -1 : this.Depth;
     while (this.Read() && (d < this.Depth)) {
         switch (this.NodeType) {
             case XmlNodeType.Element:
                 xtw.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceURI);
                 xtw.QuoteChar = this.QuoteChar;
                 xtw.WriteAttributes(this, defattr);
                 if (this.IsEmptyElement) {
                     xtw.WriteEndElement();
                 }
                 break;
             case XmlNodeType.Text:
                 xtw.WriteString(this.Value);
                 break;
             case XmlNodeType.Whitespace:
             case XmlNodeType.SignificantWhitespace:
                 xtw.WriteWhitespace(this.Value);
                 break;
             case XmlNodeType.CDATA:
                 xtw.WriteCData(this.Value);
                 break;
             case XmlNodeType.EntityReference:
                 xtw.WriteEntityRef(this.Name);
                 break;
             case XmlNodeType.XmlDeclaration:
             case XmlNodeType.ProcessingInstruction:
                 xtw.WriteProcessingInstruction(this.Name, this.Value);
                 break;
             case XmlNodeType.DocumentType:
                 xtw.WriteDocType(this.Name, this.GetAttribute("PUBLIC"), this.GetAttribute("SYSTEM"), this.Value);
                 break;
             case XmlNodeType.Comment:
                 xtw.WriteComment(this.Value);
                 break;
             case XmlNodeType.EndElement:
                 xtw.WriteFullEndElement();
                 break;
         }
     }
     if (d == this.Depth && this.NodeType == XmlNodeType.EndElement) {
         Read();
     }
 }