Solved

xml namespaces in c# - need to selectsinglenodes

Posted on 2009-03-31
6
1,780 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
ID: 24034430
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
ID: 24034588
Try a relative path:

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

Expert Comment

by:Anurag Thakur
ID: 24034757
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:absok
ID: 24039849
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
ID: 24040140
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
ID: 24040608
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

23 Experts available now in Live!

Get 1:1 Help Now