Read XML file and make values to a string. C#
Hi, I googled and couldn“t find it. If someone knows how to do that:$
My XML File
Code:
<?xml version="1.0" encoding="utf-8" ?>
<xml>
<serverip>127.0.0.1,1337</serverip>
<username>admin</username>
<password>123456</password>
<database>mydb</database>
</xml>
Here is the code to read a XML file but I can't find how I
make from <serverip>127.0.0.1,1337</serverip>
a string that echo's 127.0.0.1,1337
Code:
//Read XML file here:)
XmlTextReader reader = new XmlTextReader("settings.xml");
while (reader.Read())
{
}
Those are the strings.
Code:
string server = "";
string username = "";
string password = "";
string db = "";
:blink:
Re: Read XML file and make values to a string. C#
Code:
XmlTextReader reader = new XmlTextReader("settings.xml");
while (reader.Read())
{
string server = reader.serverip;
string username = reader.username;
string password = reader.password;
string db = reader.database;
}
See if that works..
(make sure you put 'using System.Xml;')
Re: Read XML file and make values to a string. C#
Code:
Error 1 'System.Xml.XmlTextReader' does not contain a definition for 'serverip' and no extension method 'serverip' accepting a first argument of type 'System.Xml.XmlTextReader' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Eigenaar\Mijn documenten\Visual Studio 2008\Projects\Query Sender\Query Sender\Form1.cs 27 40 Query Sender
Code:
Error 5 The name 'server' does not exist in the current context C:\Documents and Settings\Eigenaar\Mijn documenten\Visual Studio 2008\Projects\Query Sender\Query Sender\Form1.cs 37 60 Query Sender
I have those errors above for all strings.. :'( But thanks that you're trying to help me:)
Hmm, I don't know how to Define serverip :P
Re: Read XML file and make values to a string. C#
This should be useful:
http://support.microsoft.com/default.aspx/kb/307548
Especially compare the complete code listing and sample output, you should be able to put something together with it.
Re: Read XML file and make values to a string. C#
@Negata
Thanks for the link, I already saw that:P But I use Windows Forms and wanna put it as a String:S
On the example is XmlNodeType.Text //Display the text in each element. I want to get the text for each of the elements. xD
Re: Read XML file and make values to a string. C#
PHP Code:
XmlTextReader reader = new XmlTextReader("settings.xml");
while (reader.Read())
{
string node = reader.name;
switch(node)
{
case "serverip" :
string server = reader.value.ToString();// The ToString() may be redundant, try it without..
break;
case "username" :
string username = reader.value.ToString();// The ToString() may be redundant, try it without..
break;
case "password" :
string password = reader.value.ToString();// The ToString() may be redundant, try it without..
break;
case "database" :
string database = reader.value.ToString(); // The ToString() may be redundant, try it without..
break;
default :
Console.WriteLine("Found an unknown field in the XML Document! Please fix the XML document, or update this program to match the document.");
break;
}
}
I don't code C#, I don't have a way to test, so expect errors and use your brain (& Negata's Resource) to make it work for you. :wink:
Re: Read XML file and make values to a string. C#
Quote:
Originally Posted by
eele
@Negata
Thanks for the link, I already saw that:P But I use Windows Forms and wanna put it as a String:S
You already saw the page and didn't find it useful? If your problem is strings, go learn some basics.
I haven't done much C#, either, but s-p-n's solution looks pretty valid to me (and can be put together with the page I referred to).
Re: Read XML file and make values to a string. C#
Quote:
Originally Posted by
Negata
You already saw the page and didn't find it useful? If your problem is strings, go learn some basics.
I haven't done much C#, either, but s-p-n's solution looks pretty valid to me (and can be put together with the page I referred to).
I found it usefull, but that was on all the pages that I found for C# Tuts and XML reading:P I'm trying to get it to work and googling but I just can't find it out:P Else just close it... It's my problem.
Re: Read XML file and make values to a string. C#
I'm using XMl to make a virus database...so here ya go, the loader part:
Code:
public static void LoadMainDB()
{
XmlTextReader txtr = new XmlTextReader(dbpath + "main.vadb");
txtr.WhitespaceHandling = WhitespaceHandling.None;
txtr.MoveToContent();
txtr.Read();
string node = txtr.Name;
while (node == "malware")
{
node = txtr.GetAttribute("name");
string vname = node;
string vtype = txtr.GetAttribute("type");
txtr.Read();
txtr.Read();
node = txtr.ReadString();
string signature = node;
vdb.Add(vname, signature);
vtdb.Add(vname, vtype);
txtr.Read();
txtr.ReadEndElement();
node = txtr.Name;
}
}
Re: Read XML file and make values to a string. C#
Google already made me happy<3
Code:
XmlDocument doc = new XmlDocument();
doc.Load("settings.xml");
XmlNodeList settings = doc.GetElementsByTagName("connection");
string server, username, password, db, ConnectionString;
foreach (XmlNode node in settings){
XmlElement connection = (XmlElement)node;
server = connection.GetElementsByTagName("serverip")[0].InnerText;
username = connection.GetElementsByTagName("username")[0].InnerText;
password = connection.GetElementsByTagName("password")[0].InnerText;
db = connection.GetElementsByTagName("database")[0].InnerText;
MessageBox.Show(server + username + password + db);
}
Everyone thanks for there help for so far:P
Mods can close this thread.