Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Getting XPath expression for an XMLNode
 

Getting XPath expression for an XMLNode

Is there an easy way to determine or get the applicable XPath expression from a Microsoft.Office.Interop.Word.XMLNode instance at runtime?  Or do I have to navigate through the XMLNodes and build the XPath string myself?
Fourge  Wednesday, September 21, 2005 6:07 AM
I have some C++ code that we use in VSTO to build the XPath for XMLNode which validates against schema etc. It is not an exact sample code that you are looking for. I will do this one time to save you time but please understand that it is the last time I am doing this - I have work to do as well.

The idea though is something like this:

// built the list of namespaces and basenames from the leaf node
// up the tree
List<string> namespaces = new List<string>;
List<string> basenames = new List<string>;
while (xmlNode.Parent() != null)
{
   string namespaceURI = xmlnode.NamespaceURI;
   string  baseName = xmlnode.BaseName;

   namespaces.Add(namespaceURI);
   basenames.Add(baseName);
}

// iterate the lists backwards and construct the XPath
StringBuilder xpath = new StringBuilder;
int idx = 0;
int count = namespaces.Count();
for(idx = count - 1 ; idx >= 0 ; idx--)
{
    xpath.Append("/" + namespaces[idx] + ":" + basenamesIdea);
}

You can of course alias the namespace to get shorter paths. This is left as an exercise for the reader.

Hope it helps,
Misha
Misha Shneerson - MSFT  Wednesday, September 21, 2005 6:13 PM
Yep, not built in support. We build XPath ourselves by navigating to the root element.
Misha Shneerson - MSFT  Wednesday, September 21, 2005 7:08 AM
Do you have a sample of that code? It would save me some time.
Fourge  Wednesday, September 21, 2005 7:13 AM
I have some C++ code that we use in VSTO to build the XPath for XMLNode which validates against schema etc. It is not an exact sample code that you are looking for. I will do this one time to save you time but please understand that it is the last time I am doing this - I have work to do as well.

The idea though is something like this:

// built the list of namespaces and basenames from the leaf node
// up the tree
List<string> namespaces = new List<string>;
List<string> basenames = new List<string>;
while (xmlNode.Parent() != null)
{
   string namespaceURI = xmlnode.NamespaceURI;
   string  baseName = xmlnode.BaseName;

   namespaces.Add(namespaceURI);
   basenames.Add(baseName);
}

// iterate the lists backwards and construct the XPath
StringBuilder xpath = new StringBuilder;
int idx = 0;
int count = namespaces.Count();
for(idx = count - 1 ; idx >= 0 ; idx--)
{
    xpath.Append("/" + namespaces[idx] + ":" + basenamesIdea);
}

You can of course alias the namespace to get shorter paths. This is left as an exercise for the reader.

Hope it helps,
Misha
Misha Shneerson - MSFT  Wednesday, September 21, 2005 6:13 PM

You can use google to search for other answers

Custom Search

More Threads

• content control tags
• The request cache-only policy does not allow a network request and the response is not found in cache.
• Selection.Move infinite loop on Word 2007 document
• sending to excell with VB
• Integrating VSTO and OMS
• Excel Sheet Switching/Hiding
• Problem of Office add-in (C#)
• c# excel graph displayed incorrectly
• Is this possible (most likely not, but have to ask)
• Hide / Show outlook item