Reputation: 17213
ok, so, i have an xml file that looks like this:
<?xml version="1.0"?>
<Users>
<User ID="1">
<nickname>Tom</nickname>
<password>a password</password>
<host>[email protected]</host>
<email>anemail</email>
<isloggedin>false</isloggedin>
<permission>10</permission>
</User>
<User ID="2">
<nickname>ohai</nickname>
<password>sercret</password>
<host>my@host</host>
<email>my@email</email>
<isloggedin>false</isloggedin>
<permission>1</permission>
</User>
<Users>
now, first, i will have a return of what their ID number is, so, ill have "2". from that, i will need to go into, and edit the fields in it, and resave the xml. so basically what i need, is to open the file, find the info for User ID="2", and resave the xml, with DIFFERENT values inside of user 2, without affecting the rest of the document.
examlpe:
<User ID="2">
<nickname>ohai</nickname>
<password>sercret</password>
<host>my@host</host>
<email>my@email</email>
<isloggedin>false</isloggedin>
<permission>1</permission>
</User>
//do the alterations here, and end up with
<User ID="2">
<nickname>ohai</nickname>
<password>somthing that is different than before</password>
<host>the most current host that they were seen as</host>
<email>my@email</email>
<isloggedin>false</isloggedin>
<permission>1</permission>
</User>
etc.
Summary: i need to open a text file, return the information via ID number, edit the information, re-save the file. without affecting anything other than user 2
~Thanks!
Upvotes: 3
Views: 2425
Reputation: 7879
You may use XmlDocument for this:
var doc = new XmlDocument();
doc.Load("1.xml");
var node = doc.SelectSingleNode(@"//User[@ID='2']");
node.SelectSingleNode("password").InnerText="terces";
doc.Save("1.xml");
Upvotes: 0
Reputation: 2198
Check out this question, should have your answer using Linq-to-XML. This involves writing out to a Console window, but the theory is the same.
Linq to XML - update/alter the nodes of an XML Document
Upvotes: 0
Reputation: 754468
There are multiple ways you could do this - this is with an XmlDocument, which works in .NET 1.x and up, and works fine as long as your XML document isn't too long:
// create new XmlDocument and load file
XmlDocument xdoc = new XmlDocument();
xdoc.Load("YourFileName.xml");
// find a <User> node with attribute ID=2
XmlNode userNo2 = xdoc.SelectSingleNode("//User[@ID='2']");
// if found, begin manipulation
if(userNo2 != null)
{
// find the <password> node for the user
XmlNode password = userNo2.SelectSingleNode("password");
if(password != null)
{
// change contents for <password> node
password.InnerText = "somthing that is different than before";
}
// find the <host> node for the user
XmlNode hostNode = userNo2.SelectSingleNode("host");
if(hostNode != null)
{
// change contents for <host> node
hostNode.InnerText = "the most current host that they were seen as";
}
// save changes to a new file (or the old one - up to you)
xdoc.Save("YourFileNameNew.xml");
}
If you're using .NET 3.5 and up, you could also check into Linq-to-XML for a probably even easier way to manipulate your XML document.
Marc
Upvotes: 4