• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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

0
hmcgeehan
Asked:
hmcgeehan
1 Solution
 
Arabia_vnCommented:
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
 
hmcgeehanAuthor Commented:
thanks

it doesn't seem to recognise StreamReader though?
0
 
hmcgeehanAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
MikeDotNet555Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now