Link to home
Start Free TrialLog in
Avatar of jimbona27
jimbona27Flag for United Kingdom of Great Britain and Northern Ireland

asked on

problem with reading an XML file using C#

Hi EE,
i have the following xml structure and I need to extract the //feed/entry/id out although I can never get it working when the
xmlns namespace is included.

Im trying to achieve the desired outcome using the c# code in the snippet section although my attemps fail.

The code needs to extract the id node, therefore I need to collect the following data out of the xml file.

here
there
test

XML structure here
=================


<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
      <title>xx</title>
      <subtitle>xx</subtitle>
      <link rel="self" type="application/atom+xml" href="xxx"/>
      <updated>2009-02-13T17:51:40.225Z</updated>
      <author>
            <name>aaa</name>
            <uri>nnn</uri>
            <email>ccc</email>
      </author>
      <id>asd</id>
      <icon>tgb</icon>
      <rights>qqqq</rights>
      <entry>
            <title>aaaa</title>
            <link href="xxxx"/>
            <id>here</id>
            <summary/>
            <updated>dffdfsddfs</updated>
            <content>pppp</content>
      </entry>
      <entry>
            <title>aaaa</title>
            <link href="xxxx"/>
            <id>there</id>
            <summary/>
            <updated>dffdfsddfs</updated>
            <content>pppp</content>
      </entry>
      <entry>
            <title>aaaa</title>
            <link href="xxxx"/>
            <id>test</id>
            <summary/>
            <updated>dffdfsddfs</updated>
            <content>pppp</content>
      </entry>
</feed>


please advise why this code isnt working.  Many thanks!
// this code is not using Linq although I have mentioned it.  I would like to get this working, with or without Linq.
 
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(@"c:\atomtestfile.xml");
XmlNodeList node = xmldoc.SelectNodes("//feed/entry/id");
 
 
// with this code, the node.Count equals 0 ???

Open in new window

Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Avatar of jimbona27

ASKER

thanks for this, although using the code below the nodeList is empty so the foreach never gets reached?
what am I doing wrong?
Thanks!

            List<RecordTypeXml> results = new List<RecordTypeXml>();
            XmlDocument xml = new XmlDocument();
            xml.Load(@"C:\atom.xml");
            XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
            ns.AddNamespace("y", "http://schemas.microsoft.com/office/project/server/webservices/ProjectDataSet/");
            XmlNodeList nodeList = xml.SelectNodes("//feed/entry/id", ns);
            
            foreach (XmlNode test in nodeList)
            {
                // never gets reached?
            }

Open in new window

i have tried changing the string to this as well although nodeList is still empty:

XmlNodeList nodeList = xml.SelectNodes("//y:feed/y:entry/y:id", ns);
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you, this seems to work now.  Can you tell me how to use this value in the app.config as this technique of {} brackets in the config is new to me.
I need to use this key from the config file to complete this problem.  I just need a tutorial link or something simple to appreciate what this means.
Many thanks,


    <add key="xx" value="/{0}/test/{1}/"/>
any ideas?
Thanks!
Just in general, accolades can be used as a replacement for string format operations. Apparently, some application that needs those values also does some string formatting. How this works is, amongst other places, explained here: http://blog.stevex.net/index.php/string-formatting-in-csharp/

Note that this has nothing to do with the app.config, but it has everything to do with strings and you can use the same curly brackets in your normal string formatting.
thanks