Solved

xml namespaces in c# - need to selectsinglenodes

Posted on 2009-03-31
6
1,782 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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