本文整理汇总了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);
}
示例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
};
}