Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C#] Loading/Writing XML Files [Intermediate]
#1
For saving crap, databases etc. Like settings files (ini).

Imports:
Code:
using System.Xml;

Code:
Code:
// Reading
            XmlDocument XMLReader = new XmlDocument();
            XMLReader.Load("Settings.xml");
            foreach (XmlNode Information in XMLReader.SelectNodes("/Information")) //Add inner Nodes /Information/Other/Other/Other
            {
                Console.WriteLine("FirstName={0}", Information.Attributes["FirstName"].InnerText);
                Console.WriteLine("LastName={0}", Information.Attributes["LastName"].InnerText);
            }

            // Writing
            XmlTextWriter XMLWriter = new XmlTextWriter("Settings.xml", null);
            XMLWriter.WriteStartElement("Information");
            XMLWriter.WriteAttributeString("FirstName", "*Your FirstName*");
            XMLWriter.WriteAttributeString("LastName", "*Your LastName*");
            XMLWriter.WriteEndElement();
            XMLWriter.Close();

Usage:
Code:
Look up ;D
Reply
#2
You don't happen to have this in Visual Basic .NET? or know of a site that can Convert it to Visual Basic??

Thanks anyway, Nice share!
Reply
#3
(09-16-2010, 07:23 AM)milopeach Wrote: You don't happen to have this in Visual Basic .NET? or know of a site that can Convert it to Visual Basic??

Thanks anyway, Nice share!

Code:
Imports System.Xml

Code:
' Reading
Dim XMLReader As New XmlDocument()
XMLReader.Load("Settings.xml")
For Each Information As XmlNode In XMLReader.SelectNodes("/Information")
    'Add inner Nodes /Information/Other/Other/Other
    Console.WriteLine("FirstName={0}", Information.Attributes("FirstName").InnerText)
    Console.WriteLine("LastName={0}", Information.Attributes("LastName").InnerText)
Next

' Writing
Dim XMLWriter As New XmlTextWriter("Settings.xml", Nothing)
XMLWriter.WriteStartElement("Information")
XMLWriter.WriteAttributeString("FirstName", "*Your FirstName*")
XMLWriter.WriteAttributeString("LastName", "*Your LastName*")
XMLWriter.WriteEndElement()
XMLWriter.Close()
Reply
#4
Thankyou SO Much Fragma!
Reply
#5
Thank you a bunch, Fragma!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [VB.NET]GeoIP Using XML[Source] Ch4ng3m3 12 5,821 07-21-2012, 11:37 AM
Last Post: G3Hawkeye
  [C#] Simple RC2 Encryption/Decryption [Intermediate] Mike 3 3,569 12-21-2010, 11:38 PM
Last Post: ZxPwn420
  [SIMPLE] Loading Screen VB [TUTORIAL] Zerb 12 5,256 12-13-2010, 09:43 PM
Last Post: Zerb
  Need Help with Registering VB Files G1™ 2 946 12-12-2010, 06:24 PM
Last Post: Flamingo
  Simple loading bar Extremespeed™ 9 2,395 12-12-2010, 04:23 PM
Last Post: Figaro

Forum Jump:


Users browsing this thread: 1 Guest(s)