tags:

views:

831

answers:

2

I used the XMLReader format:


XmlReader xmlReader = XmlReader.Create("batch.xml");
while (xmlReader.Read())
{
    //Keep reading
    if (xmlReader.Name.Equals("Keyword") && (xmlReader.NodeType == XmlNodeType.Element))
    {
        // get attribute from the Xml element here
        string keywords = xmlReader.GetAttribute("name"); 
    }
}

How do I cast "keywords" as a String[]?

+1  A: 

It depends.

If your XML has a single name attribute that contains multiple keywords, call String.Split, like this:

string[] keywords = xmlReader.GetAttribute("name").Split(' ');

If you have multiple name attributes or Keyword elements, create a List<string> and fill it up in a loop, like this:

List<string> keywords = new List<string>();
XmlReader xmlReader = XmlReader.Create("batch.xml");
while (xmlReader.Read()) {
    //Keep reading
    if (xmlReader.Name.Equals("Keyword") && (xmlReader.NodeType == XmlNodeType.Element))
        keywords.Add(xmlReader.GetAttribute("name")); 
}

If you really need a string[], you can call keywords.ToArray().

SLaks
I finally hit 10K! Thanks Mohit.
SLaks
A: 

Since you're using a XmlReader, you can't have all nodes at once. You need to create a List<string> collection, populate and return it.

Rubens Farias