Solved

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

Posted on 2014-10-21
4
277 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
  • 2
4 Comments
 
LVL 32

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 32

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 32

Expert Comment

by:it_saige
ID: 40395929
Not a problem.

-saige-
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Where to begin studying? 5 66
C# Update Status Bar.... 1 24
C# Application Local DB Connection String 23 61
Image(7) 1 37
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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now