當前位置: 首頁>>代碼示例>>C#>>正文


C# XmlTextReader.ReadRequiredAttributeValue方法代碼示例

本文整理匯總了C#中System.Xml.XmlTextReader.ReadRequiredAttributeValue方法的典型用法代碼示例。如果您正苦於以下問題:C# XmlTextReader.ReadRequiredAttributeValue方法的具體用法?C# XmlTextReader.ReadRequiredAttributeValue怎麽用?C# XmlTextReader.ReadRequiredAttributeValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Xml.XmlTextReader的用法示例。


在下文中一共展示了XmlTextReader.ReadRequiredAttributeValue方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: FromStream

        /// <summary>
        /// Loads a TextureFont from a stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="loadTextureFunc"></param>
        /// <returns></returns>
        public static SpriteSheet FromStream(GraphicsDevice graphicsDevice, Stream stream, Func<string, Color?, Texture> loadTextureFunc)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (stream == null)
                throw new ArgumentNullException("stream");

            if (loadTextureFunc == null)
                throw new ArgumentNullException("loadTextureFunc");

            graphicsDevice.EnsureDeviceCreated();

            List<Rectangle> rectangles = new List<Rectangle>();
            string textureFile = null;
            Color colorKey = Color.Transparent;

            try
            {
                using (var xml = new XmlTextReader(stream))
                {
                    xml.WhitespaceHandling = WhitespaceHandling.None;

                    xml.Read();

                    if (xml.NodeType == XmlNodeType.XmlDeclaration)
                        xml.Read();

                    if (xml.NodeType != XmlNodeType.Element && xml.Name != "SpriteSheet")
                        throw new XmlException("Invalid SpriteSheet xml file.");

                    textureFile = xml.ReadRequiredAttributeValue("Texture");
                    colorKey = Color.FromHexString(xml.ReadAttributeValueOrDefault("BackgroundColor", "00000000"));

                    xml.Read();
                    while (xml.Name == "Frame")
                    {
                        Rectangle rectangle = new Rectangle(
                            xml.ReadRequiredAttributeValue<int>("X"),
                            xml.ReadRequiredAttributeValue<int>("Y"),
                            xml.ReadRequiredAttributeValue<int>("Width"),
                            xml.ReadRequiredAttributeValue<int>("Height"));

                        rectangles.Add(rectangle);
                        xml.Read();
                    }
                }
            }
            catch (XmlException ex)
            {
                throw new GraphicsException("An error occured while parsing the SpriteSheet xml file.", ex);
            }

            Texture texture = loadTextureFunc(textureFile, colorKey);

            if (texture == null)
                throw new InvalidOperationException("loadTextureFunc returned null.");

            return new SpriteSheet(texture, rectangles);
        }
開發者ID:smack0007,項目名稱:Snowball_v1,代碼行數:67,代碼來源:SpriteSheet.cs

示例2: FromStream

        /// <summary>
        /// Loads a TextureFont from a stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="loadTextureFunc"></param>
        /// <returns></returns>
        public static TextureFont FromStream(GraphicsDevice graphicsDevice, Stream stream, Func<string, Color?, Texture> loadTextureFunc)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (stream == null)
                throw new ArgumentNullException("stream");

            if (loadTextureFunc == null)
                throw new ArgumentNullException("loadTextureFunc");

            graphicsDevice.EnsureDeviceCreated();

            Dictionary<char, Rectangle> rectangles = new Dictionary<char, Rectangle>();
            string textureFile = null;
            Color backgroundColor = Color.Transparent;
            string fontName = TextureFont.DefaultFontName;
            int fontSize = TextureFont.DefaultFontSize;
            int characterSpacing;
            int lineSpacing;

            try
            {
                using (var xml = new XmlTextReader(stream))
                {
                    xml.WhitespaceHandling = WhitespaceHandling.None;

                    xml.Read();

                    if (xml.NodeType == XmlNodeType.XmlDeclaration)
                        xml.Read();

                    if (xml.NodeType != XmlNodeType.Element && xml.Name != "TextureFont")
                        throw new XmlException("Invalid TextureFont xml file.");

                    textureFile = xml.ReadRequiredAttributeValue("Texture");

                    backgroundColor = Color.FromHexString(xml.ReadAttributeValueOrDefault("BackgroundColor", "FFFFFFFF"));
                    fontName = xml.ReadAttributeValueOrDefault("FontName", DefaultFontName);
                    fontSize = xml.ReadAttributeValueOrDefault<int>("FontSize", DefaultFontSize);
                    characterSpacing = xml.ReadAttributeValueOrDefault<int>("CharacterSpacing", DefaultCharacterSpacing);
                    lineSpacing = xml.ReadAttributeValueOrDefault<int>("LineSpacing", DefaultLineSpacing);

                    xml.Read();
                    while (xml.Name == "Character")
                    {
                        Rectangle rectangle = new Rectangle(
                            xml.ReadRequiredAttributeValue<int>("X"),
                            xml.ReadRequiredAttributeValue<int>("Y"),
                            xml.ReadRequiredAttributeValue<int>("Width"),
                            xml.ReadRequiredAttributeValue<int>("Height"));

                        rectangles.Add(xml.ReadRequiredAttributeValue("Value")[0], rectangle);
                        xml.Read();
                    }
                }
            }
            catch (XmlException ex)
            {
                throw new GraphicsException("An error occured while parsing the TextureFont xml file.", ex);
            }

            Texture texture = loadTextureFunc(textureFile, backgroundColor);

            if (texture == null)
                throw new InvalidOperationException("loadTextureFunc returned null.");

            return new TextureFont(texture, rectangles, fontName, fontSize)
            {
                CharacterSpacing = characterSpacing,
                LineSpacing = lineSpacing
            };
        }
開發者ID:smack0007,項目名稱:Snowball_v1,代碼行數:80,代碼來源:TextureFont.cs


注:本文中的System.Xml.XmlTextReader.ReadRequiredAttributeValue方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。