XML - How to convert a VBA code into C#

Hi, would appreciate your help to the following issue:

I like to extract data values from an xml file with the following format:
<context id="c00246">
    <identifier scheme="http://www.sec.gov/CIK">0000850693</identifier>

Open in new window

I am able to extract the instant value i.e. 2010-12-31 using the following vba code:
strContextPeriod = oInstance.SelectSingleNode("//xbrli:context[@id='" & strContextID & "']/xbrli:period/xbrli:instant").Text where strContextID  = "c00246"

Open in new window

I would like to find the equivalent code in C#.

Specifications: Visual Studio 2013, System.Xml, XmlTextReader reader = new XmlTextReader(textBox1.Text);

I tried with the following codes:
while (reader.Read())
                switch (reader.NodeType)
                              case XmlNodeType.Element: // The node is an element.
                        if (reader.Name.IndexOf("[b]context[/b]", 0) == 0)
                                      textBox4.AppendText(reader.Name + "====" + reader.GetAttribute("period") + "]" + reader.ReadInnerXml() + "\r\n"); 

Open in new window

and only successful in extracting out the context blocks. Would need assistance to extract the instant value only. Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
You could use either LINQ-to-XML XDocument or System.Xml.XmlDocument and get similar syntax:

var document = new XDocument();
var parentElement = document.Descendants("Parent").FirstOrDefault();

Open in new window

var document = new XmlDocument();
var parentElement = document.SelectSingleNode("//Parent");

Open in new window

RussellbrownAuthor Commented:
Thank you for the reply. I have the tried the second suggestion and came up with mixed results:

1. when using a simple xml file i.e.
XmlDocument doc = new XmlDocument();
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/catalog/book");

Open in new window

I am able to get the correct nodes.count.

2. when using an actual URL:

var nodes = doc.SelectSingleNode("/context");

Open in new window

XmlNodeList nodes = doc.DocumentElement.SelectNodes("/xbrl/context");

Open in new window

I got a null nodes.count. Have tried several permutations for the xpath  i.e. //xbrl/context etc but not able to achieve a proper extraction.
Bob LearnedCommented:
Try this:

XmlNodeList nodes = doc.DocumentElement.SelectNodes("//context");

This will get you the <context> element no matter where it is in the hierarchy.
RussellbrownAuthor Commented:
Ran the code:
XmlDocument doc = new XmlDocument();
XmlNodeList nodes = doc.DocumentElement.SelectNodes("//context");

Open in new window

I still get:
Bob LearnedCommented:
You have to think about namespaces for each of the nodes.  Sometimes, the namespace prefixes are implied.  The trick is to find the namespace that is used for <context> element.  In order to associate namespaces, you would need to use an XmlNamespaceManager.

My guess is xbrl and http://www.xbrl.org/2003/instance.  

<xbrl xmlns="http://www.xbrl.org/2003/instance" xmlns:link="http://www.xbrl.org/2003/linkbase" 
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:asi="http://allergan.com/20110228"
xmlns:xbrldi="http://xbrl.org/2006/xbrldi" xmlns:iso4217="http://www.xbrl.org/2003/iso4217" 
xmlns:us-gaap="http://xbrl.us/us-gaap/2009-01-31" xmlns:agn="http://allergan.com/20101231"

Open in new window

Here is some code to highlight using an XmlNamespaceManager, and using the prefix to select the node.

var doc = new XmlDocument();
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("app", "http://www.weather.gov/forecasts/xml/OGC_services");
var firstPoint = doc.SelectSingleNode("//app:Forecast_Gml2Point", nsmgr);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.