Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get a value from an XML file?

Posted on 2009-12-18
5
Medium Priority
?
256 Views
Last Modified: 2012-05-08
Hi
I have an xml file (see its contents below)

Anyone know code which (given a full path to an xml file) and the field name 'for example HEADLINE' it will return the value?

thanks
<?xml version="1.0" encoding="UTF-8"?>
<document>
	<attributes>
		<field name="DOKNR" stn="DOCID">340913</field>
		<field name="SOURCEID">977</field>
		<field name="SOURCEL">Kildare Post</field>
		<field name="EDITION"/>
		<field name="SDATE" stn="SDATE" format="ddmmyyyy">29042009</field>
		<field name="CDATE" stn="SOURCE" format="ddmmyyyy">27052009</field>
		<field name="PAGE" stn="SPAGE">25</field>
		<field name="HEADLINE" stn="HEADLINE">On the beat</field>
		<field name="SUBTITLE"/>
		<field name="AUTHOR"/>

Open in new window

0
Comment
Question by:hmcgeehan
5 Comments
 
LVL 8

Expert Comment

by:Arabia_vn
ID: 26080538
If my memory serves me well , it's something like this


XmlDocument xdoc = new XmlDocument();
           try
           {
               StreamReader textFile = new StreamReader("YourPathString");
               string fileContents = textFile.ReadToEnd();
               textFile.Close();
               xdoc.LoadXml(fileContents);
           }
           catch (Exception ex) { };

 XmlNodeList nodeListResult = xdoc.SelectNodes("document/attributes/field[@name=['HEADLINE']");
0
 

Author Comment

by:hmcgeehan
ID: 26080563
thanks

it doesn't seem to recognise StreamReader though?
0
 

Author Comment

by:hmcgeehan
ID: 26080620
ok I used System.IO.StreamReader ...

Now I get ...

Expression must evaluate to a node-set.
at MS.Internal.Xml.XPath.XPathParser.ParseNodeTest(AstNode qyInput, AxisType axisType, XPathNodeType nodeType) at MS.Internal.Xml.XPath.XPathParser.ParseStep(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseUnionExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseMultiplicativeExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseAdditiveExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelationalExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseEqualityExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseStep(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParsePathExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseUnionExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseMultiplicativeExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseAdditiveExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelationalExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseEqualityExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseOrExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseXPathExpresion(String xpathExpresion) at MS.Internal.Xml.XPath.QueryBuilder.Build(String query, Boolean allowVar, Boolean allowKey) at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver) at System.Xml.XPath.XPathNavigator.Select(String xpath) at System.Xml.XmlNode.SelectNodes(String xpath) at AnPostWebParts.Templates.MediaCoverageAdminResultsTemplate.titleHL_DataBinding(Object sender, EventArgs e)
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 26080783
The following worked for me:
        static void Main(string[] args)
        {
            XmlDocument xdoc = new XmlDocument();

            xdoc.Load("C:\\test.xml");

            XmlNodeList nodeListResult = xdoc.SelectNodes("document/attributes/field[@name='HEADLINE']");

            foreach (XmlNode result in nodeListResult)
                Console.WriteLine(result.InnerXml);

            Console.ReadKey();
        }
    }

Open in new window

0
 
LVL 3

Expert Comment

by:MikeDotNet555
ID: 26082179
I like LINQ -TO-SQL, very powerfull.

You gotta add using System.Xml.Linq
XElement xmlFile = XElement.Load("c:\\rac.xml");
                            string SOURCEID = xmlFile.Element("attributes").Elements("field").Single(n=>n.Attribute("name").Value == "SOURCEID").Value;
                            Console.WriteLine("SOURCEID=" + SOURCEID);

Open in new window

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

577 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