jimbona27
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</u pdated>
<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</updat ed>
<content>pppp</content>
</entry>
<entry>
<title>aaaa</title>
<link href="xxxx"/>
<id>there</id>
<summary/>
<updated>dffdfsddfs</updat ed>
<content>pppp</content>
</entry>
<entry>
<title>aaaa</title>
<link href="xxxx"/>
<id>test</id>
<summary/>
<updated>dffdfsddfs</updat ed>
<content>pppp</content>
</entry>
</feed>
please advise why this code isnt working. Many thanks!
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
<updated>2009-02-13T17:51:
<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</updat
<content>pppp</content>
</entry>
<entry>
<title>aaaa</title>
<link href="xxxx"/>
<id>there</id>
<summary/>
<updated>dffdfsddfs</updat
<content>pppp</content>
</entry>
<entry>
<title>aaaa</title>
<link href="xxxx"/>
<id>test</id>
<summary/>
<updated>dffdfsddfs</updat
<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 ???
https://www.experts-exchange.com/questions/23017732/C-How-to-find-Elements-from-XML-file.html?cid=236&anchorAnswerId=20462218#a20462218
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!
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?
}
ASKER
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);
XmlNodeList nodeList = xml.SelectNodes("//y:feed/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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}/"/>
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}/"/>
ASKER
any ideas?
Thanks!
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.
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.
ASKER
thanks