Solved

xml namespaces in c# - need to selectsinglenodes

Posted on 2009-03-31
6
1,775 Views
Last Modified: 2013-11-08
I am needing to go through this collection of xml elements and extract data from child nodes of each element. But I am only about halfway getting there. What am I missing?

///Here is how I have set up the xml doc with a typed dataset and all good:
               
               XmlDataDocument xmlDoc = new XmlDataDocument(dsReturnResults);
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema-instance");
                XmlElement root = xmlDoc.DocumentElement;
                XmlNodeList nlList = root.GetElementsByTagName("Entry");

The nlList contains all the nodes I need, I can see the children with all the data I am after but I just cannot access it to save my sanity. Here is a sample of what I've attempted:

foreach (XmlNode entry in nlList)
                {
                  string strTest2 = entry.SelectNodes("/xsd:sdnEntry/xsd:sdnType", nsmgr).ToString(); /// this gives me nothing

                  string strType = entry.ChildNodes.Item(2).FirstChild.InnerText;///this works fine but does me no good
                  string strTest =  entry.ChildNodes.Item(1).FirstChild.InnerText;///since the indexes will vary depending on "Type"
             }

Any guidance much appreciated.
0
Comment
Question by:absok
6 Comments
 
LVL 15

Expert Comment

by:oobayly
Comment Utility
Seeing as you're trying to select child nodes of an Entry node, you shouldn't have to specify an absolute path in the xpath query.
From  "/xsd:sdnEntry/xsd:sdnType"
To "xsd:sdnEntry/xsd:sdnType"

If that doesn't do it, can you post a snippet of the xml?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
Try a relative path:

entry.SelectNodes("./xsd:sdnType", nsmgr) // dot before slash
0
 
LVL 26

Expert Comment

by:Anurag Thakur
Comment Utility
in the following EE link i have shown how to do selectNodes similarly you can do select single node with a namespace
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23017732.html?cid=236#a20462218
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:absok
Comment Utility
Maybe I haven't defined the nsmgr correctly? Here is the xml with a sample node:

<?xml version="1.0" standalone="yes" ?>
- <sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
- <publshInformation>
  <Publish_Date>03/04/2009</Publish_Date>
  <Record_Count>4208</Record_Count>
  </publshInformation>

- <sdnEntry>
  <uid>2675</uid>
  <firstName>Toby</firstName>
  <lastName>Letterman</lastName>
  <title>CEO</title>
  <sdnType>Individual</sdnType>
- <programList>
  <program>SDT</program>
  </programList>
- <dateOfBirthList>
- <dateOfBirthItem>
  <uid>1001</uid>
  <dateOfBirth>03 May 1973</dateOfBirth>
  <mainEntry>true</mainEntry>
  </dateOfBirthItem>
  </dateOfBirthList>
- <placeOfBirthList>
- <placeOfBirthItem>
  <uid>1002</uid>
  <placeOfBirth>Amarillo, TX</placeOfBirth>
  <mainEntry>true</mainEntry>
  </placeOfBirthItem>
  </placeOfBirthList>
  </sdnEntry>
- <sdnEntry>


My end goal is to filter a typed data set based on a name search criteria. I don't think I understand why using "GetElementsByTagName" works to get the nodelist with no nsmgr info, probably because I always used "SelectNodes" before.

Is there just a better way to filter this list? Someone has suggested a DataViewManager approach.

Thanks

E
0
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
Comment Utility
The way the XmlNamespaceManager works with default namespaces isn't that clear. Basically, you've got two namespaces in the xml, one of them with a prefix, the other without.
Also, I notice in your first post that the namespace prefix you've used is xsd, whereas in the xml it's xsd.

Anyhow, this should do the trick. Notice that the I've given my own prefix "sdn" for the sdnList namespace, and have used that in the xpath queries. Also note that all my queries are done relative to the node being queried.

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.LoadXml(xml);

    XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);

    nsMgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

    nsMgr.AddNamespace("sdn", "http://tempuri.org/sdnList.xsd");
 

    XmlElement root = xmlDoc.DocumentElement;
 

    foreach (XmlNode entry in root.SelectNodes("sdn:sdnEntry", nsMgr)) {

      string strTest2 = entry.SelectNodes("sdn:sdnType", nsMgr).ToString(); /// this gives me nothing
 

      string strType = entry.ChildNodes.Item(2).FirstChild.InnerText;///this works fine but does me no good

      string strTest = entry.ChildNodes.Item(1).FirstChild.InnerText;///since the indexes will vary depending on "Type"

    }

Open in new window

0
 

Author Comment

by:absok
Comment Utility
Thank You! Yesterday's pain is today's joy - I just had to use "SelectSingleNode" instead of "SelectNodes" and grab the innerxml and it is working like a champ now.

string strTypeTest4 = entry.SelectSingleNode("sdn:sdnType", nsMgr).InnerText;
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now