Add XmlNode to XmlElement

Roger picture Roger · May 30, 2011 · Viewed 12.4k times · Source

I get a soap envelope back from a web service with customer data such as name and address etc. The address does not contain city/suburb but postcode. I have all the city and suburbs with their post codes in a CSV file so I want to insert the correct name for each post code. I can store it in a database or something else but this is more about how to insert the node before I pass the data on.

The code is like :

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(searchResponse);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("ns", wsNamespace);

XmlNodeList postCodeNodes = xDoc.SelectNodes("//ns:postcode", nsmgr);
string applicationPath = AppDomain.CurrentDomain.BaseDirectory;

foreach (XmlNode node in postCodeNodes)
{ 
    using (StreamReader readFile = new StreamReader(applicationPath + "postcodes.csv"))
    {
        string line;
        string[] row;

        while ((line = readFile.ReadLine()) != null)
        {
                row = line.Split(',');
                if (row[0].ToString() == node.InnerText)
                {
                    string suburb = row[1].ToString();
                    //XmlNode ndSuburb = xDoc.CreateElement("suburb");
                    //ndSuburb.Value = suburb;
                    //node.ParentNode.AppendChild(ndSuburb);
                    break;
                }
        }
    }
}

and I am not sure what to do where I have commented out the code. Any suggestions? Tips on how to make this more efficient would also be appreciated.

Thanks in advance.

Answer

Thorsten Dittmar picture Thorsten Dittmar · May 30, 2011

Well, it's a bit hard to know without actually seeing the XML structure that exists and the desired new XML structure. Basically I'd assume that you want a new XML node containing the suburb at the same level as the postcode element.

In that case, I'd used:

XmlElement elem = xDoc.CreateElement("suburb");
elem.InnerText = ...;
node.ParentNode.AppendChild(elem);

EDIT
As for efficiency: Why don't you read your "postcode file" only once, adding the entries to a dictionary that contains the post code as a key and the suburb as a value? That's far quicker than reading the file every time.

Dictionary<string, string> postCodeMap = new Dictionary<string, string>();
string[] lines = File.ReadAllLines(...);
foreach (string line in lines)
{
   string[] parts = line.Split(',');
   postCodeMap[parts[0]] = parts[1];
}

And later do:

foreach (XmlNode node in postCodeNodes)
{ 
    string suburb = postCodeMap[node.InnerText];

    XmlElement elem = xDoc.CreateElement("suburb");
    elem.InnerText = suburb;
    node.ParentNode.AppendChild(elem);
}