Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Easy but urgent XML reading

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
dkloeck
Asked:
dkloeck
  • 3
1 Solution
 
MogalManicCommented:
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
 
dkloeckAuthor Commented:
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
 
dkloeckAuthor Commented:
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
 
dkloeckAuthor Commented:
hehe, it gives the right values I just wrote 2000 as year =))

it works now, thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now