hi i want to get a arralist from a xml file with Linq to XML
here my xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<modules>
<modul name=""></modul>
<modul name="P1:"></modul>
<modul name="AS5050:"></modul>
<modul name="GMS4010:"></modul>
<modul name="GMS4020:"></modul>
<modul name="GMS4030A:"></modul>
<modul name="gateway:"></modul>
<modul name="view_only:"></modul>
</modules>
here my code:
private ArrayList GetModules()
{
XDocument x = XDocument.Load(Server.MapPath(@"~\App_Data\UGNX.xml"));
ArrayList data = from item in x.Elements("modul") ///???
return data;
}
I suspect you want:
private List<string> GetModules() {
return XDocument.Load(Server.MapPath(@"~\App_Data\UGNX.xml")
.Root // Root element
.Elements("modul") // Select child "modul" elements
.Attributes("name") // Select "names" attributes within them
.Select(attribute => (string) attribute) // Fetch the values
.ToList(); // Convert to a list of strings
}
I'd strongly recommend the use of a generic collection here rather than ArrayList
.
Make sure that you understand each line of this solution, so that in the future you can come up with similar code yourself.
See more on this question at Stackoverflow