1

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[]?

2 Answers 2

1

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().

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.