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
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"/>
ASKER
thanks
it doesn't seem to recognise StreamReader though?
it doesn't seem to recognise StreamReader though?
ASKER
ok I used System.IO.StreamReader ...
Now I get ...
Expression must evaluate to a node-set.
at MS.Internal.Xml.XPath.XPat hParser.Pa rseNodeTes t(AstNode qyInput, AxisType axisType, XPathNodeType nodeType) at MS.Internal.Xml.XPath.XPat hParser.Pa rseStep(As tNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseRelativ eLocationP ath(AstNod e qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rsePathExp r(AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseUnionEx pr(AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseMultipl icativeExp r(AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseAdditiv eExpr(AstN ode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseRelatio nalExpr(As tNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseEqualit yExpr(AstN ode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseAndExpr (AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseOrExpr( AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseStep(As tNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseRelativ eLocationP ath(AstNod e qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseRelativ eLocationP ath(AstNod e qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseRelativ eLocationP ath(AstNod e qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rsePathExp r(AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseUnionEx pr(AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseMultipl icativeExp r(AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseAdditiv eExpr(AstN ode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseRelatio nalExpr(As tNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseEqualit yExpr(AstN ode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseAndExpr (AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseOrExpr( AstNode qyInput) at MS.Internal.Xml.XPath.XPat hParser.Pa rseXPathEx presion(St ring xpathExpresion) at MS.Internal.Xml.XPath.Quer yBuilder.B uild(Strin g query, Boolean allowVar, Boolean allowKey) at System.Xml.XPath.XPathExpr ession.Com pile(Strin g xpath, IXmlNamespaceResolver nsResolver) at System.Xml.XPath.XPathNavi gator.Sele ct(String xpath) at System.Xml.XmlNode.SelectN odes(Strin g xpath) at AnPostWebParts.Templates.M ediaCovera geAdminRes ultsTempla te.titleHL _DataBindi ng(Object sender, EventArgs e)
Now I get ...
Expression must evaluate to a node-set.
at MS.Internal.Xml.XPath.XPat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I like LINQ -TO-SQL, very powerfull.
You gotta add using System.Xml.Linq
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);
XmlDocument xdoc = new XmlDocument();
try
{
StreamReader textFile = new StreamReader("YourPathStri
string fileContents = textFile.ReadToEnd();
textFile.Close();
xdoc.LoadXml(fileContents)
}
catch (Exception ex) { };
XmlNodeList nodeListResult = xdoc.SelectNodes("document