Solved

Easy but urgent XML reading

Posted on 2006-06-19
4
230 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now