Solved

Easy but urgent XML reading

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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