Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

xml namespaces in c# - need to selectsinglenodes

Posted on 2009-03-31
6
Medium Priority
?
1,799 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 30

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

971 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