Displaying xml item in a listbox (XlmSerialiser)
I have the fol开发者_开发问答lowing deserialize method:
public static List<Enquete> GetAlleEnquetes()
{
XmlReader reader = new XmlTextReader(HttpContext.Current.Server.MapPath("~/App_Data/Questions.xml"));
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Enquete>), new XmlRootAttribute("enqueteSysteem"));
return (List<Enquete>)(serializer.Deserialize(reader));
}
finally
{
reader.Close();
}
}
public static Enquete GetEnqueteName(string name)
{
foreach (Enquete e in GetAllEnquetes())
{
if (e.Name == name)
return e;
}
return null;
}
Which works properly (this code is located in a class withing App_data).
Next i want to retrieve all the names and display them in a listbox.
But how exactly do i retrieve the names? <Enquete Name ="">
and list all of those in my listbox control? (through the asp.cs file)
My xml structure looks like this:
<enqueteSystem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Enquete Name="test">
<Questions>
<Question QuestionText="testtest" QuestionType="na"></Question>
</Questions>
</Enquete>
</enqueteSystem>
to retrieve all the names
var serializedEnquetes = XDocument.Parse(serializedXml);
IEnumerable<string> names = serializedEnquetes
.Descendants("Enquete")
.Attributes("Name")
.Select(a => a.Value);
Then simply use the 'names' collection as a source for your listbox
精彩评论