First of all, I think Regex should be just fine.
But if you really want to use an XML parser I love XmlDocument/XmlNode in .NET. The two functions SelectSingleNode and SelectNodes are infinitely useful. Unfortunately, I do not have a Word XML example in front of me, so let's assume this XML:
<Document>
<MergeField name="phone"></MergeField>
<MergeField name="email"></MergeField>
</Document>
You would then use code as follows:
XmlDocument wordDoc = new XmlDocument();
wordDoc.Load(fileName);
XmlNodeList mergeNodes = wordDoc.SelectNodes("//MergeField");
foreach(XmlNode mergeNode in mergeNodes)
{
string fieldName = mergeNode.Attributes["name"].Value;
// Do something here based on field name
// e.g.:
mergeNode.InnerText = GetFieldValue(fielName);
}
doc.Save(fileName);
The tricky part is that Word XML uses XML namespaces all over the place, so you need to use the XmlNamespaceManager class is .NET to tell the XML document which namespace is which, so it would be more like:
XmlDocument wordDoc = new XmlDocument();
wordDoc.Load(fileName);
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("o", "http://somenamepaceurl.com");
XmlNodeList mergeNodes = wordDoc.SelectNodes("//o:MergeField", nsm);
foreach(XmlNode mergeNode in mergeNodes)
{
string fieldName = mergeNode.Attributes["name"].Value;
// Do something here based on field name
// e.g.:
mergeNode.InnerText = GetFieldValue(fielName);
}
doc.Save(fileName);