Solved

c# XmlDocument node elements/attributes not visible from known nodes

Posted on 2014-10-21
4
294 Views
Last Modified: 2014-10-21
Techies--
I am trying to export an xml document from a Dictionary/hastable in odataSF.ReadFromDictinaryByKey("Position") where "Position" is a string key. The line xdoc.Save(Console.Out) works. The output of the data is in the attached file.
               XmlDocument xdoc = new XmlDocument();
               xdoc = odataSF.ReadFromDictionaryByKey("Position");
               xdoc.Save(Console.Out);

               Console.WriteLine("\n");

Open in new window

     
                    
In the next set of lines, I look to see what the nodes are. The nodes that output from here are "xml" and "feed".

			   
               //capture nodes - start at the very beginning "/"
               List<string> nodeNames = new List<string>();
               foreach (System.Xml.XmlNode node in xdoc.SelectNodes("/"))
               {
                   foreach (System.Xml.XmlNode child in node.ChildNodes)
                   {
                       if (!nodeNames.Contains(child.Name)) nodeNames.Add(child.Name);
                       
                   }
               }
			   
               Console.WriteLine("Names of Nodes: \n");
               foreach (string name in nodeNames)
               {
                   Console.WriteLine("Node: " + name + "\n");
               }

Open in new window


Here is where I am attempting to get specific values back out of the document. No errors, but nothing is found. Doesn't matter whether I put in xml, feed, etc. Nothing
is found.

               XmlNodeList xnList = xdoc.SelectNodes("/xml");
               foreach (XmlNode xn in xnList)
               {
                   string jobTitle = xn["jobTitle"].InnerText;
                   string code = xn["code"].InnerText;

                   Console.WriteLine("Elements: {0} {1}", jobTitle, code);
               }

Open in new window


I am confused about the structure of this doc--I thought that I would see the nodes: feed/entry/content/properties-- with the elements under properties.
Can someone offer direction/advice on what it is I haven't got right?
Positions.xml
0
Comment
Question by:ditallop
[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
  • 2
4 Comments
 
LVL 34

Expert Comment

by:ste5an
ID: 40395796
The problem is that yor XML uses a default namespace. Thus your selector can not find it, cause it does not know it. Here is an example showing how to specify it.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40395844
Try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

static class XmlExample
{
	public static void Main()
	{
		XmlReader reader = XmlReader.Create("Feeds.xml");
		XElement data = XElement.Load(reader);
		XmlNameTable table = reader.NameTable;
		XmlNamespaceManager manager = new XmlNamespaceManager(table);
		manager.AddNamespace("ns", "http://www.w3.org/2005/Atom");
		manager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
		manager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
		manager.AddNamespace("base", "https://api8.successfactors.com:443/odata/v2/");
		foreach (XElement entries in data.XPathSelectElements("./ns:entry", manager))
		{
			foreach (var contents in (from entry in entries.Elements() where entry.Name.LocalName.Contains("content") select entry))
			{
				foreach (var properties in (from content in contents.Elements() where content.Name.LocalName.Contains("properties") select content))
				{
					foreach (var property in (from property in properties.Elements() where property.Name.LocalName.Contains("jobTitle") || property.Name.LocalName.Contains("code") select property))
						Console.WriteLine(string.Format("Name - {0}; Value - {1}", property.Name.LocalName, property.Value));
				}
			}
		}
		Console.ReadLine();
	}
}

Open in new window

Produces the following output:Capture.JPG
-saige-
0
 

Author Comment

by:ditallop
ID: 40395886
Brilliantly done @Saige! :)
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40395929
Not a problem.

-saige-
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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