Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 13th, 2006, 02:06 PM
Authorized User
 
Join Date: Jun 2005
Location: Kathmandu, , Nepal.
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to connect2sandep
Default reading a XML file

well, i read a lot about xml but never used it, and i need help regarding a problem. Here is some XML in string form:
<order>
<transactiondetails>
<oid>1a6493b4-7cfe-4c33-bdc1-238bb4af7fe0</oid>
</transactiondetails>
<payment>
<chargetotal>40.00</chargetotal>
</payment>
<billing>
<name>Sandeep Pradhananga</name>
<company>NA</company>
<address2>KTM, </address2>
<state>MD</state>
<zip>56536</zip>
<address1>KTM, </address1>
<city>KTM</city>
</billing>
<orderoptions>
<ordertype>SALE</ordertype>
<result>LIVE</result>
</orderoptions>
<creditcard>
<cvmvalue>123</cvmvalue>
<cvmindicator>provided</cvmindicator>
<cardnumber>4111-1111-1111-1111</cardnumber>
<cardexpyear>08</cardexpyear>
<cardexpmonth>01</cardexpmonth>
</creditcard>
<merchantinfo>
<configfile>1001158260</configfile>
</merchantinfo>
</order>

assuming the xml is in a string object how to parse the value corresponding to 'chargetotal'using C#.
Please Help!


Reply With Quote
  #2 (permalink)  
Old December 14th, 2006, 04:50 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

There are a few different ways, the easiest is probably to load the string into an XPathDocument via a StringReader, if the XML is a file you can use a StreamReader instead. Then use the XPathNavigator to select the desired value.
The code for a console application is shown here:
Code:
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace XmlFromString
{
  class Program
  {
    static void Main(string[] args)
    {
      string xml = "<order/>"; //Full order document to go here
      XPathDocument doc = new XPathDocument(new StringReader(xml));
      string xpath = "/*/payment/chargetotal";
      XPathNavigator nav = doc.CreateNavigator();
      XPathNavigator node = nav.SelectSingleNode(xpath);
      Console.WriteLine("{0}: {1}", node.Name, node.InnerXml);
      Console.ReadLine();
    }
  }
}
--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with PHP file reading XML file for output rydog65 Beginning PHP 0 March 26th, 2008 05:13 PM
Reading xml file which is in a JAR file pradheepayyanar J2EE 2 December 26th, 2007 05:52 AM
reading a XML file connect2sandep General .NET 0 December 13th, 2006 02:10 PM
Reading from a xml file and writing to another billy_bob_the_3rd XML 5 April 29th, 2005 01:40 PM
Help with reading huge XML file hitesh_kapadia ASP.NET 1.x and 2.0 Application Design 0 August 6th, 2004 02:54 PM



All times are GMT -4. The time now is 05:39 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.