Solved

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

Posted on 2014-10-21
4
292 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 33

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 33

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 33

Expert Comment

by:it_saige
ID: 40395929
Not a problem.

-saige-
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

756 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