当前位置: 首页>>代码示例>>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;未经允许,请勿转载。