Solved

Easy but urgent XML reading

Posted on 2006-06-19
4
237 Views
Last Modified: 2010-04-16
Hi ther, here goes a very easy question (I have absolut no idea about xml lol )

I have an XML that looks like that
<?xml version="1.0" encoding="UTF-8"?>
<LayerSet Name="ZippedShapeFile" ShowOnlyOneLayer="false" ShowAtStartup="true">
 <!--Some nodes inside Layerset-->
</LayerSet>
<TimeStamp>
     <year>2000</year>
     <month>09</month>
     <day>22</day>
     <hour>6</hour>
     <minute>30</minute>
    <timeZone>CDT</timeZone>
</TimeStamp>

I just need to read the Timestamp and get it as a DateTime

Thanks in advance =)
0
Comment
Question by:dkloeck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 16933372
The simplest way is use the XmlDocument class (in the System.Xml namespace)

   XmlDocument doc=new XmlDocument();
   doc.load(filename);  //use .loadXml(str) to load xml stored in string

   string year=doc.SelectSingleNode("/LayerSet/TimeStamp/year").Text;
   string month=doc.SelectSingleNode("/LayerSet/TimeStamp/month").Text;
   string day=doc.SelectSingleNode("/LayerSet/TimeStamp/day").Text;
   string hour=doc.SelectSingleNode("/LayerSet/TimeStamp/hour").Text;
   string minute=doc.SelectSingleNode("/LayerSet/TimeStamp/minute").Text;
   string timeZone=doc.SelectSingleNode("/LayerSet/TimeStamp/timeZone").Text;

   DateTime date=new DateTime(
       int.Parse(year),
       int.Parse(month),
       int.Parse(day),
       int.Parse(hour),
       int.parse(minute),
       0);  //no seconds...


0
 
LVL 10

Author Comment

by:dkloeck
ID: 16933492
There was no .Text, so i supposed you meant .InnerText
and timestamp was not in LayerSet so i changed the Node path from ("/LayerSet/TimeStamp/xxx") to ("/TimeStamp/xxx")
now it looks like this:

XmlDocument doc=new XmlDocument();
                  doc.Load(xmlPath);  //use .loadXml(str) to load xml stored in string

                  string year=doc.SelectSingleNode("/TimeStamp/year").InnerText;
                  string month=doc.SelectSingleNode("/TimeStamp/month").InnerText;
                  string day=doc.SelectSingleNode("/TimeStamp/day").InnerText;
                  string hour=doc.SelectSingleNode("/TimeStamp/hour").InnerText;
                  string minute=doc.SelectSingleNode("/TimeStamp/minute").InnerText;
                  string timeZone=doc.SelectSingleNode("/TimeStamp/timeZone").InnerText;

                  DateTime date=new DateTime(
                        int.Parse(year),
                        int.Parse(month),
                        int.Parse(day),
                        int.Parse(hour),
                        int.Parse(minute),
                        0);  //no seconds...
                  System.Windows.Forms.MessageBox.Show(date.ToString());

but it gives me an error "There are multiple root elements. Line 47, position 2."

is it not possible to have layerset and timestamp as roots in the xml?
0
 
LVL 10

Author Comment

by:dkloeck
ID: 16933512
ok..i changed the position of timestamp to inside of layerset.. it works now, but innertext seem to be wrong, it gives out wrong values
0
 
LVL 10

Author Comment

by:dkloeck
ID: 16933590
hehe, it gives the right values I just wrote 2000 as year =))

it works now, thanks
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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