System.Xml.XmlReader.ReadOuterXml C# (CSharp) Method

ReadOuterXml() public method

public ReadOuterXml ( ) : string
return string
        public virtual string ReadOuterXml()
        {
            if (ReadState != ReadState.Interactive)
            {
                return string.Empty;
            }
            if ((this.NodeType != XmlNodeType.Attribute) && (this.NodeType != XmlNodeType.Element))
            {
                Read();
                return string.Empty;
            }

            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
            XmlWriter xtw = CreateWriterForInnerOuterXml(sw);

            try
            {
                if (this.NodeType == XmlNodeType.Attribute)
                {
                    xtw.WriteStartAttribute(this.Prefix, this.LocalName, this.NamespaceURI);
                    WriteAttributeValue(xtw);
                    xtw.WriteEndAttribute();
                }
                else
                {
                    xtw.WriteNode(this, false);
                }
            }
            finally
            {
                xtw.Close();
            }
            return sw.ToString();
        }

Usage Example

 /// <summary>
 /// convert the xml representation of this class into the document and metadata properties
 /// </summary>
 /// <param name="reader"></param>
 public void ReadXml(XmlReader reader)
 {
    // good place for debug logging
     try
     {
         // take the reader, turn into an XmlDocument, then parse out the metadata and documents
         XmlDocument xmldoc = new XmlDocument();
         xmldoc.LoadXml(reader.ReadOuterXml());
         submissionMetadata = xmldoc.SelectSingleNode(@".//*[local-name()='SubmitObjectsRequest']");     // cheating and ignoring namespaces
         if (submissionMetadata == null)
         {
             // good place for debug logging
             throw new Exception("P&RRequestXDSB Reader cannot find SubmitObjectsRequest");
         }
         XmlNodeList theDocs = xmldoc.SelectNodes(@".//*[local-name()='Document']");
         if (theDocs == null)
         {
             // good place for debug logging
             throw new Exception("P&RRequestXDSB Reader cannot find Document");
         }
         foreach (XmlNode aDoc in theDocs)
         {
             SubmissionDocument theDoc = new SubmissionDocument();
             theDoc.documentID = aDoc.Attributes["id"].Value.ToString();
             theDoc.documentText = Convert.FromBase64String(aDoc.InnerText);
             submissionDocuments.Add(theDoc);
         }
     }
     catch (Exception ex)
     {
         // good place for debug logging
         throw ex;
     }
     // good place for debug logging
 }
All Usage Examples Of System.Xml.XmlReader::ReadOuterXml