Solved

xml namespaces in c# - need to selectsinglenodes

Posted on 2009-03-31
6
1,786 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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