Link to home
Start Free TrialLog in
Avatar of hmcgeehan
hmcgeehan

asked on

Get a value from an XML file?

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

Avatar of Arabia_vn
Arabia_vn
Flag of Viet Nam image

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']");
Avatar of hmcgeehan
hmcgeehan

ASKER

thanks

it doesn't seem to recognise StreamReader though?
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)
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America 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
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