Пример #1
0
        public static XmlLiteDocument LoadFromStream(Stream objStream)
        {
            if (objStream == null)
            {
                throw new ArgumentNullException("objStream", "A valid non-null Stream is required.");
            }

            XmlLiteDocument objXmlDocument = null;

            try
            {
                XmlReaderSettings objReaderSettings = new XmlReaderSettings();
                objReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;

                using (XmlReader objXmlReader = XmlReader.Create(objStream, objReaderSettings))
                {
                    objXmlDocument = LoadFromReader(objXmlReader);
                }
            }
            catch (Exception objException)
            {
                ExceptionHelper objExceptionHelper = new ExceptionHelper(objException);
                string          strErrorMessage    = objExceptionHelper.GetDetailedErrorMessage("The following error was encountered while building the xml document for the supplied stream:\n");
                throw new Exception(strErrorMessage);
            }

            return(objXmlDocument);
        }
Пример #2
0
        public static XmlLiteDocument LoadFromFile(string strFilePath)
        {
            if ((strFilePath == null) || (strFilePath.Length == 0))
            {
                throw new ArgumentException("A valid path to the file is required..", "strFilePath");
            }
            bool blnFileExists = File.Exists(strFilePath);

            if (blnFileExists == false)
            {
                throw new FileNotFoundException("The specified file does not exist", strFilePath);
            }

            XmlLiteDocument objXmlDocument = null;

            try
            {
                using (FileStream objFileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    objXmlDocument = LoadFromStream(objFileStream);
                }
            }
            catch (Exception objException)
            {
                ExceptionHelper objExceptionHelper = new ExceptionHelper(objException);
                string          strErrorMessage    = objExceptionHelper.GetDetailedErrorMessage("The following error was encountered while building the xml document for '" + strFilePath + "':\n");
                throw new Exception(strErrorMessage);
            }

            return(objXmlDocument);
        }
Пример #3
0
        public static XmlLiteDocument LoadFromXml(string strXml)
        {
            if ((strXml == null) || (strXml.Length == 0))
            {
                throw new ArgumentException("The supplied xml value cannot contain null or an empty string.", "strFilePath");
            }

            XmlLiteDocument objXmlDocument = null;

            try
            {
                using (StringReader objStringReader = new StringReader(strXml))
                {
                    XmlReaderSettings objReaderSettings = new XmlReaderSettings();
                    objReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;

                    using (XmlReader objXmlReader = XmlReader.Create(objStringReader, objReaderSettings))
                    {
                        objXmlDocument = LoadFromReader(objXmlReader);
                    }
                }
            }
            catch (Exception objException)
            {
                ExceptionHelper objExceptionHelper = new ExceptionHelper(objException);
                string          strErrorMessage    = objExceptionHelper.GetDetailedErrorMessage("The following error was encountered while parsing the following xml:\n" + strXml + "\n\n");
                throw new Exception(strErrorMessage);
            }

            return(objXmlDocument);
        }
Пример #4
0
        public static string ToXml(XmlLiteElement objXmlLiteElement)
        {
            if (objXmlLiteElement == null)
            {
                throw new ArgumentNullException("objXmlLiteElement", "A valid non-null XmlLiteElement is required.");
            }

            XmlLiteDocument objXmlLiteDocument = new XmlLiteDocument(objXmlLiteElement);

            return(objXmlLiteDocument.ExportToXml());
        }
Пример #5
0
        public static XmlLiteDocument ToXml(IXmlConvertible objXmlConvertible, string strElementName)
        {
            if (objXmlConvertible == null)
            {
                throw new ArgumentNullException("objXmlConvertible", "A valid non-null IXmlConvertible is required.");
            }

            XmlLiteDocument objXmlLiteDocument = new XmlLiteDocument(strElementName);

            try
            {
                objXmlConvertible.WriteXml(objXmlLiteDocument.Root);
            }
            catch (Exception objException)
            {
                string strErrorMessage = "Unable to generate XML for the message type '" + objXmlConvertible.GetType().FullName + ".";
                throw new Exception(strErrorMessage, objException);
            }

            return(objXmlLiteDocument);
        }
Пример #6
0
        public static TObjectType FromXml <TObjectType>(string strXml)
            where TObjectType : IXmlConvertible, new()
        {
            if (strXml == null)
            {
                throw new ArgumentNullException("strXml", "A valid non-null string is required.");
            }

            XmlLiteDocument objXmlLiteDocument = null;

            try
            {
                objXmlLiteDocument = XmlLiteDocument.LoadFromXml(strXml);
            }
            catch (Exception objException)
            {
                string strErrorMessage = "Unable to load the XML fragment:\n" + strXml;
                throw new Exception(strErrorMessage, objException);
            }

            return(FromXml <TObjectType>(objXmlLiteDocument));
        }
Пример #7
0
        public static TObjectType FromXml <TObjectType>(XmlLiteDocument objXmlLiteDocument)
            where TObjectType : IXmlConvertible, new()
        {
            if (objXmlLiteDocument == null)
            {
                throw new ArgumentNullException("objXmlLiteDocument", "A valid non-null XmlLiteDocument is required.");
            }

            TObjectType objObject = default(TObjectType);

            try
            {
                objObject = new TObjectType();
                objObject.ReadXml(objXmlLiteDocument.Root);
            }
            catch (Exception objException)
            {
                string strErrorMessage = "Unable to create message from XML.";
                throw new Exception(strErrorMessage, objException);
            }

            return(objObject);
        }
Пример #8
0
        public static XmlLiteDocument LoadFromReader(XmlReader objXmlReader)
        {
            if (objXmlReader == null)
            {
                throw new ArgumentNullException("objXmlReader", "The xml reader must represent a valid instance.");
            }

            int                    intDepth          = 0;
            XmlLiteElement         objRootElement    = null;
            XmlLiteElement         objCurrentElement = null;
            Stack <XmlLiteElement> objElements       = new Stack <XmlLiteElement>();

            while (objXmlReader.EOF == false)
            {
                objXmlReader.Read();
                switch (objXmlReader.NodeType)
                {
                case XmlNodeType.Element:

                    intDepth          = objXmlReader.Depth;
                    objCurrentElement = new XmlLiteElement(objXmlReader.LocalName);
                    if (objRootElement == null)
                    {
                        objRootElement = objCurrentElement;
                        objElements.Push(objCurrentElement);
                    }
                    else
                    {
                        XmlLiteElement objParentElement = objElements.Peek();
                        objParentElement.Elements.Add(objCurrentElement);

                        if (objXmlReader.IsEmptyElement == false)
                        {
                            objElements.Push(objCurrentElement);
                        }
                    }
                    if (objXmlReader.HasAttributes == true)
                    {
                        while (objXmlReader.MoveToNextAttribute() == true)
                        {
                            objCurrentElement.Attributes.Add(objXmlReader.Name, objXmlReader.Value);
                        }
                    }

                    break;

                case XmlNodeType.Attribute:
                    objCurrentElement.Attributes.Add(objXmlReader.Name, objXmlReader.Value);
                    break;

                case XmlNodeType.EndElement:
                    objElements.Pop();
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.CDATA:

                    if (intDepth == objXmlReader.Depth)
                    {
                        XmlLiteElement objParentElement = objElements.Peek();
                        objParentElement.Value = objXmlReader.Value;
                    }
                    else
                    {
                        objCurrentElement.Value = objXmlReader.Value;
                    }
                    break;

                default:
                    break;
                }
            }

            XmlLiteDocument objDocument = new XmlLiteDocument(objRootElement);

            return(objDocument);
        }