Results 1 to 15 of 15

Thread: XML help please...

  1. #1

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    XML help please...

    Lets start off by saying I HATE the documentation for XML. All other documentation is easy for me...maybe I really need to spend some time learning more about this XML stuff...

    Now with that out of my system, I can ask the question. I have an xml document, I need to get the values individually into different string variables.

    Here is the XML Document:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <server>
             <connectionstring>myConnection</connectionstring>
             <smtpserver>myServer</smtpserver>
        </server>
        <administrator>
             <username>hellswraith</username>
             <password>mypassword</password>
             <firstname>Brian</firstname>
             <lastname>Russell</lastname>
             <email>[email protected]</email>
             <birthdate>4/11/1976</birthdate>
        </administrator>
    </configuration>
    So all I want is to open it, access each node independently and put its value in variables. Here are the variables I want to end up with:
    Code:
    string connectionString = ?;
    string smtpServer = ?;
    string adminUserName = ?;
    string adminPassword = ?;
    string firstName = ?;
    string lastName = ?;
    string emailAddress = ?;
    DateTime birthDate = ?;
    I have no idea how to do this. I have tried like 10 different ways from what I have found on the Internet, but none just access the node, and give back a value based on the nodes name I give it. This seems like it should be super easy!

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You can use a DataSet. Its simple but very effective. Here is a demo I did. Its built using VS.NET 2003.
    Attached Files Attached Files
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Thanks for the help, I do appreciate it. So all I really need to read in values is a dataset.

    Is there any way to just access different pieces of the xml document?

    I will go with your way, it seems very easy to do.

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Yes you can use xpath but thats a whole different story. I have little experience using xpath.
    Dont gain the world and lose your soul

  5. #5

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I just wanted to come back and tell you that the code you gave works perfectly.

    I like the dataset way...it seems real simple.

    Anyway, it seems like there would be an xml object that you could just call a method passing in the name of the element you wanted, and it would return the result. Seems like it should be that simple..lol. I just don't know much about XML yet to fully understand why it is so complex.

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Anyway, it seems like there would be an xml object that you could just call a method passing in the name of the element you wanted, and it would return the result.
    XML DOM let's you perform somewhat sql like statements against an xml document via xpath expressions. Checkout the XmlDocument class in the System.Xml namespace. You can use methods like 'SelectSingleNode', where you pass in the name of the node you want. XPath/XSLT is a really powerful language and not to difficult to learn.

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Example:

    Code:
    using System;
    using System.Xml;
    
    class XPathExample
    {
    	/* <applicationSettings>
    	 *     <userId>Lethal</userId>
    	 *     <password>CSharp</password>
    	 * </applicationSettings> */	 
    	static void Main() 
    	{
    		XmlDocument source = new XmlDocument();
    		source.Load(@"c:\settings.xml");
    		string message = "User ID: " + source.DocumentElement.SelectSingleNode("userId").InnerText + '\n';
    		message += "Password: " + source.DocumentElement.SelectSingleNode("password").InnerText;
    		Console.WriteLine(message);
    	}
    }

  8. #8

    Thread Starter
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Thanks Lethal. That was what I was trying to do all along. It seemed like there would be an easy way to do it.

    Thanks both of you.

  9. #9
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    and how to write an xml file?
    \m/\m/

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Is this what you mean PT Exorcist ?

    Code:
    DataSet ds =new DataSet ;
    ds.WriteXml (path);

  11. #11
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tks! that made the trick..but i can see accessing XML can be a little slow..or not? also, which process is faster? using for example a database like mysql or a xml based database? i am willing to start learning about this topic so i have a few questions about it
    \m/\m/

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    tks! that made the trick..but i can see accessing XML can be a little slow..or not? also, which process is faster? using for example a database like mysql or a xml based database? i am willing to start learning about this topic so i have a few questions about it
    Well ... I work with XMLs only when I need quick operations . Otherwise , it's efficient to work with database . XML is slower esp when you are dumping the whole dataset content to it .It's easy to learn (actually basics stuff you have to know ) . Did this answer you question !

  13. #13
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm yes thanks, databases like mysql seem like a pain to learn ..bah
    \m/\m/

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by PT Exorcist
    hmm yes thanks, databases like mysql seem like a pain to learn ..bah
    What's wrong MS Access , it's more more easier than SQL Stuff .

  15. #15
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    just had the idea of learning mysql because thats what everyone is learning..
    \m/\m/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width