如何使用 XML 类在网格视图或列表视图中显示 XML 内容
本文关键字:XML 视图 列表 显示 内容 网格 何使用 | 更新日期: 2023-09-27 18:37:15
我从网上得到了一个XML文件,用于我的学校,这个xml文件是Kongregate的文件,用于在我的网站中嵌入游戏。XML 文件采用 ELEMENT 格式,但网格视图和列表视图需要采用 ATTRIBUTE 格式。这是 xml 文件的链接:http://www.kongregate.com/games_for_your_site.xml
我有一个关于把它变成一个类的建议,所以我做了,但我仍然不知道如何使用它。这是类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Class1
/// </summary>
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class gameset
{
private gamesetGame[] gameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("game")]
public gamesetGame[] game
{
get
{
return this.gameField;
}
set
{
this.gameField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class gamesetGame
{
private uint idField;
private string titleField;
private string thumbnailField;
private System.DateTime launch_dateField;
private string categoryField;
private string featured_imageField;
private string[] screenshotField;
private string flash_fileField;
private ushort widthField;
private ushort heightField;
private string urlField;
private string descriptionField;
private string instructionsField;
private string developer_nameField;
private uint gameplaysField;
private decimal ratingField;
private ushort id1Field;
/// <remarks/>
public uint id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
/// <remarks/>
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
/// <remarks/>
public string thumbnail
{
get
{
return this.thumbnailField;
}
set
{
this.thumbnailField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
public System.DateTime launch_date
{
get
{
return this.launch_dateField;
}
set
{
this.launch_dateField = value;
}
}
/// <remarks/>
public string category
{
get
{
return this.categoryField;
}
set
{
this.categoryField = value;
}
}
/// <remarks/>
public string featured_image
{
get
{
return this.featured_imageField;
}
set
{
this.featured_imageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("screenshot")]
public string[] screenshot
{
get
{
return this.screenshotField;
}
set
{
this.screenshotField = value;
}
}
/// <remarks/>
public string flash_file
{
get
{
return this.flash_fileField;
}
set
{
this.flash_fileField = value;
}
}
/// <remarks/>
public ushort width
{
get
{
return this.widthField;
}
set
{
this.widthField = value;
}
}
/// <remarks/>
public ushort height
{
get
{
return this.heightField;
}
set
{
this.heightField = value;
}
}
/// <remarks/>
public string url
{
get
{
return this.urlField;
}
set
{
this.urlField = value;
}
}
/// <remarks/>
public string description
{
get
{
return this.descriptionField;
}
set
{
this.descriptionField = value;
}
}
/// <remarks/>
public string instructions
{
get
{
return this.instructionsField;
}
set
{
this.instructionsField = value;
}
}
/// <remarks/>
public string developer_name
{
get
{
return this.developer_nameField;
}
set
{
this.developer_nameField = value;
}
}
/// <remarks/>
public uint gameplays
{
get
{
return this.gameplaysField;
}
set
{
this.gameplaysField = value;
}
}
/// <remarks/>
public decimal rating
{
get
{
return this.ratingField;
}
set
{
this.ratingField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute("id")]
public ushort id1
{
get
{
return this.id1Field;
}
set
{
this.id1Field = value;
}
}
}
你能告诉我如何在列表视图中显示数据吗?或者您可以将我的 xml 文件从元素转换为属性吗?
您需要将fileds更改为属性,然后在反序列化xml文件后,您有一个列表,然后将其绑定到列表视图像任何数据一样。
YourListView.DataSource = theListOfXmlItems;
YourListView.DataBind();
您可以在此处找到一个简单的教程
我能够通过更改我的类来使其工作。我之前拥有的类是通过使用特殊的复制 XML 到类获得的类。我认为我写它的方式就是 Thorarnis 的意思(将字段更改为属性)
这是类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
/// <summary>
/// Summary description for GameXML
/// </summary>
[XmlRoot("gameset")]
public class GameSet
{
[XmlElement("game")]
public List<Game> Game { get; set; }
}
[XmlRoot("game")]
public class Game
{
[XmlElement("id")]
public int ID { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("thumbnail")]
public string Thumbnail { get; set; }
[XmlElement("launch_date")]
public string Launch { get; set; }
[XmlElement("category")]
public string Category { get; set; }
[XmlElement("flash_file")]
public string Flash { get; set; }
[XmlElement("width")]
public string Width { get; set; }
[XmlElement("height")]
public string Height { get; set; }
[XmlElement("url")]
public string Url { get; set; }
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("instructions")]
public string Instructions { get; set; }
[XmlElement("developer_name")]
public string Developer_name{ get; set; }
[XmlElement("gameplays")]
public string Gameplays { get; set; }
[XmlElement("rating")]
public string Rating { get; set; }
}