Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » [C#] Reading an XML File


[C#] Reading an XML File

Reply
  #1 (permalink)  
Old 09-25-10, 11:23 PM
Lol5916 Lol5916 is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
[C#] Reading an XML File

I'm trying to read two specific details from an XML file, both on the second line: 1.00 and com.melonmobile.android.acm .

Here is the XML file.
XML | <?xml version="1.0" encoding=" - Anonymous - yKBJfNan - Pastebin.com

Here is what I have so far. (Not much)
Code:
XmlDocument doc = new XmlDocument();
doc.Load("AndroidManifest.xml");
I've messed around, but I'm not sure what to do from here?
Reply With Quote
  #2 (permalink)  
Old 09-26-10, 10:00 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Hi,

Here is what you need:

Code:
XmlDocument doc = new XmlDocument();
            doc.Load("AndroidManifest.xml");


            XmlNodeList nlist1 = doc.ChildNodes;
            XmlNode node1 = nlist1.Item(1);

            string versionName = node1.Attributes["android:versionName"].Value;
            string package = node1.Attributes["package"].Value;
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
The Following User Says Thank You to Yeroon For This Useful Post:
Lol5916 (09-26-10)
  #3 (permalink)  
Old 09-26-10, 10:39 AM
Lol5916 Lol5916 is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks a lot!
Reply With Quote
  #4 (permalink)  
Old 09-26-10, 11:56 AM
Lol5916 Lol5916 is offline
Newbie Coder
 
Join Date: Aug 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Sorry for the double post, but I can't seem to edit my posts.

How would I go about checking if:
Code:
string versionName = node1.Attributes["android:versionName"].Value;
is null, before setting it as a string?
Reply With Quote
  #5 (permalink)  
Old 09-27-10, 02:07 PM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Hi,

Something like this. Bit overkill if the node never is empty, but to give you an idea:

Code:
XmlDocument doc = new XmlDocument();
            doc.Load("XMLFile1.xml");


            XmlNodeList nodeList;
            XmlNodeList nlist1;
            XmlNode node1;

            nlist1 = doc.ChildNodes;
            node1 = nlist1.Item(1);

            if (node1 != null)
            {
                if (node1.Attributes != null)
                {
                    if(node1.Attributes["android:versionName"] != null)
                    {
                        string versionName = node1.Attributes["android:versionName"].Value;
                    }
                    if (node1.Attributes["package"] != null)
                    {
                        string package = node1.Attributes["package"].Value;
                    }
                }
            }
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #6 (permalink)  
Old 09-27-10, 11:03 PM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
If you also need to check if it's not empty you could use this

Code:
                    if(!string.IsNullOrEmpty(node1.Attributes["android:versionName"]))
                    {
                        string versionName = node1.Attributes["android:versionName"].Value;
                    }
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML PHP MYSQL- Writing to a xml file punky79 PHP 8 05-18-09 04:21 AM
Hello all, can you help me? K4ot1K Other Languages 2 01-23-09 05:23 AM
Reading file path from file imatrox05 Visual Basic 1 12-20-06 10:01 AM
reading a remote xml file into a variable bdee1 PHP 1 12-20-05 12:32 PM
Perl Script to parse 1 xml file into many flsh Perl 0 08-09-04 10:21 AM


All times are GMT -5. The time now is 02:19 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.