Solved

Easy but urgent XML reading

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fomat Json objects 6 28
What's the most efficient way to load up a couple of large table in C#? 5 33
Parse the OuterHTML Data in the txtfile using c# 4 47
Error in script 11 45
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

21 Experts available now in Live!

Get 1:1 Help Now