/// <summary>
		/// Parses XML from a
		/// <see cref="string"/>
		/// ,
		/// fixing the illegal control character optionally.
		/// </summary>
		/// <param name="input">a <code>String</code> containing the XMP packet</param>
		/// <param name="options">the parsing options</param>
		/// <returns>Returns an XML DOM-Document.</returns>
		/// <exception cref="Com.Adobe.Xmp.XMPException">Thrown when the parsing fails.</exception>
		private static XmlDocument ParseXmlFromString(string input, ParseOptions options)
		{
			InputSource source = new InputSource(new Sharpen.StringReader(input));
			try
			{
				return ParseInputSource(source);
			}
			catch (XMPException e)
			{
				if (e.GetErrorCode() == XMPErrorConstants.Badxml && options.GetFixControlChars())
				{
					source = new InputSource(new FixASCIIControlsReader(new Sharpen.StringReader(input)));
					return ParseInputSource(source);
				}
				else
				{
					throw;
				}
			}
		}
		/// <summary>Runs the XML-Parser.</summary>
		/// <param name="source">an <code>InputSource</code></param>
		/// <returns>Returns an XML DOM-Document.</returns>
		/// <exception cref="Com.Adobe.Xmp.XMPException">Wraps parsing and I/O-exceptions into an XMPException.</exception>
        private static XmlDocument ParseInputSource(InputSource source)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(source.GetStream());

                return doc;
            }
            catch (XmlException e)
            {
                throw new XMPException("XML parsing failure", XMPErrorConstants.Badxml, e);
            }
            catch (IOException e)
            {
                throw new XMPException("Error reading the XML-file", XMPErrorConstants.Badstream, e);
            }
            catch (Exception e)
            {
                throw new XMPException("XML Parser not correctly configured", XMPErrorConstants.Unknown, e);
            }
        }
		/// <summary>
		/// Parses XML from a byte buffer,
		/// fixing the encoding (Latin-1 to UTF-8) and illegal control character optionally.
		/// </summary>
		/// <param name="buffer">a byte buffer containing the XMP packet</param>
		/// <param name="options">the parsing options</param>
		/// <returns>Returns an XML DOM-Document.</returns>
		/// <exception cref="Com.Adobe.Xmp.XMPException">Thrown when the parsing fails.</exception>
		private static XmlDocument ParseXmlFromBytebuffer(ByteBuffer buffer, ParseOptions options)
		{
			InputSource source = new InputSource(buffer.GetByteStream());
			try
			{
				return ParseInputSource(source);
			}
			catch (XMPException e)
			{
				if (e.GetErrorCode() == XMPErrorConstants.Badxml || e.GetErrorCode() == XMPErrorConstants.Badstream)
				{
					if (options.GetAcceptLatin1())
					{
						buffer = Latin1Converter.Convert(buffer);
					}
					if (options.GetFixControlChars())
					{
						try
						{
							string encoding = buffer.GetEncoding();
							System.IO.StreamReader fixReader = new FixASCIIControlsReader(new InputStreamReader(buffer.GetByteStream(), encoding));
							return ParseInputSource(new InputSource(fixReader));
						}
						catch (UnsupportedEncodingException)
						{
							// can normally not happen as the encoding is provided by a util function
							throw new XMPException("Unsupported Encoding", XMPErrorConstants.Internalfailure, e);
						}
					}
					source = new InputSource(buffer.GetByteStream());
					return ParseInputSource(source);
				}
				else
				{
					throw;
				}
			}
		}