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