1

I am trying to get file locations out of an XML file rather than hard coding locations that could ultimately change.

I don't know how many files there will be which is causing me issue.

How can I get this function to iterate through all nodes and copy them to a new index within the array?

update

Is there a way to do this using arrays if you don't know the size, perhaps an ArrayList for example or chuck it into a list and convert to an array after? Whichever method would be best.

Below is the code I've written so far:

public static void LoadMyXML()
{
    string xmlfp = _AppPath + @"\Config";
    try
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(xmlfp);
        XmlNode xmlLst = xDoc.SelectSingleNode("parent1/child1");
        string[] xmlNodeArray;

        foreach (XmlNode node in xmlLst.ChildNodes)
        {
            //read through and add any child nodes to the array
            xmlNodeArray[] = node.InnerText;
        }
    }
    catch(Exception ex)
    {
        WriteErrorToLog("XML Loading error " + ex.ToString());
    }
}

1 Answer 1

5

You can use a List, if you don't know the size:

List<string> xmlNodes = new List<string>();

foreach (XmlNode node in xmlLst.ChildNodes)
{
    xmlNodes.Add(node.InnerText);
}

Apart from that I would suggest you use new Linq to XML API for this.

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

1 Comment

Thanks, was considering list but not really used arrays so wanted to venture out of my comfort zone - is there no way to do this using arrays if you don't know the size? ArrayList for example or chuck it into a list and convert to an array after?

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.